-
Notifications
You must be signed in to change notification settings - Fork 51
/
spoj_Mixtures.cpp
68 lines (54 loc) · 1.01 KB
/
spoj_Mixtures.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<climits>
using namespace std;
int a[1000];
int dp[1000][1000];
int sum(int s, int e){
int ans =0;
for(int i=s; i<=e; i++){
ans += a[i];
ans %= 100;
}
return ans;
}
int solveMixture(int i, int j){
//base case
if(i>=j){
return 0;
}
//lookup
if(dp[i][j]!=-1){
return dp[i][j];
}
dp[i][j] = INT_MAX;
for(int k=i; k<=j; k++){
dp[i][j] = min(dp[i][j], solveMixture(i,k) + solveMixture(k+1,j) + sum(i,k)*sum(k+1,j));
}
return dp[i][j];
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0; i<n; i++){
cin>>a[i];
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
dp[i][j] = -1;
}
}
cout<<solveMixture(0,n-1)<<endl;
}
//Test cases and outputs
/* 2
2
18 19
342
3
40 60 20
2400 */
return 0;
}