-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d05c33b
commit c438baa
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
#include<climits> | ||
using namespace std; | ||
|
||
int a[1000]; | ||
long long dp[1000][1000]; | ||
|
||
long long sum(int s,int e){ | ||
long long ans = 0; | ||
for(int i=s;i<=e;i++){ | ||
ans += a[i]; | ||
ans %= 100; | ||
} | ||
return ans; | ||
} | ||
|
||
long long solveMixtures(int i,int j){ | ||
//Base case | ||
if(i>=j){ | ||
return 0; | ||
} | ||
|
||
//If the answer already there | ||
if(dp[i][j]!=-1){ | ||
return dp[i][j]; | ||
} | ||
//We to break our expression at every possible k | ||
|
||
dp[i][j] = INT_MAX; | ||
for(int k=i;k<=j;k++){ | ||
dp[i][j] = min(dp[i][j], solveMixtures(i,k)+solveMixtures(k+1,j)+sum(i,k)*sum(k+1,j)); | ||
} | ||
return dp[i][j]; | ||
} | ||
|
||
|
||
int main() { | ||
|
||
int n; | ||
|
||
while((scanf("%d",&n))!=EOF){ | ||
|
||
//Read N integers | ||
for(int i=0;i<n;i++){ | ||
cin>>a[i]; | ||
} | ||
|
||
//Init dp with -1 | ||
for(int i=0;i<=n;i++){ | ||
for(int j=0;j<=n;j++){ | ||
dp[i][j] = -1; | ||
} | ||
} | ||
cout<<solveMixtures(0,n-1)<<endl; | ||
|
||
} | ||
|
||
} |