-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKnuth Division.cpp
70 lines (55 loc) · 1.53 KB
/
Knuth Division.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
69
70
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
const int N = 5005;
ll dp[N][N];
int opt[N][N];
ll ara[N];
ll cumsum[N];
ll recurse(int l,int r){
if( l>r ) return -1;
if( l == r ) return dp[l][r] = 0;
if( dp[l][r] != -1 ) return dp[l][r];
for(int k=l+1;k<=r;k++){
ll a = dp[l][k-1],b=dp[k][r];
if( a == -1 ) a = recurse(l,k-1);
if( b == -1 ) b = recurse(k,r);
if( dp[l][r] == -1 ) dp[l][r] = a + b + cumsum[r] - cumsum[l-1];
else dp[l][r] = min(dp[l][r], a + b + cumsum[r] - cumsum[l-1]);
}
return dp[l][r] ;
}
int main(){
FASTIO;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>ara[i];
cumsum[i] = ara[i]+cumsum[i-1];
}
for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp[i][j] = -1;
auto C = [&](int i, int j) {
return cumsum[j]-cumsum[i-1];
};
for (int i = 0; i < N; i++) {
opt[i][i] = i;
dp[i][i] = 0;
}
for (int i = n-1; i > 0; i--) {
for (int j = i+1; j <= n; j++) {
ll mn = LONG_MAX;
ll cost = C(i, j);
for (int k = opt[i][j-1]; k <= min(j-1, opt[i+1][j]); k++) {
if (mn >= dp[i][k] + dp[k+1][j] + cost) {
opt[i][j] = k;
mn = dp[i][k] + dp[k+1][j] + cost;
}
}
dp[i][j] = mn;
}
}
cout<<dp[1][n];
}