-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMixtures-SPOJ.cpp
35 lines (34 loc) · 1.14 KB
/
Mixtures-SPOJ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
int minimumSmoke(const std::vector<int>& mixtures) {
int n = mixtures.size();
std::vector<std::vector<int>> dp(n, std::vector<int>(n));
std::vector<std::vector<int>> smoke(n, std::vector<int>(n));
for (int len = 2; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int j = i + len - 1;
dp[i][j] = std::numeric_limits<int>::max();
for (int k = i; k < j; k++) {
int currSmoke = smoke[i][k] + smoke[k + 1][j] + mixtures[i] * mixtures[k + 1];
if (dp[i][j] > dp[i][k] + dp[k + 1][j] + currSmoke) {
dp[i][j] = dp[i][k] + dp[k + 1][j] + currSmoke;
smoke[i][j] = (mixtures[i] + mixtures[k + 1]) % 100;
}
}
}
}
return dp[0][n - 1];
}
int main() {
int n;
std::cin >> n;
std::vector<int> mixtures(n);
for (int i = 0; i < n; i++) {
std::cin >> mixtures[i];
}
int minSmoke = minimumSmoke(mixtures);
std::cout << minSmoke << std::endl;
return 0;
}