-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathnucleul valoros 2.cpp
52 lines (48 loc) · 1.29 KB
/
nucleul valoros 2.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
# include <bits/stdc++.h>
# define NR 3005
using namespace std;
ifstream f("nucleulvaloros2.in");
ofstream g("nucleulvaloros2.out");
int i,j,n,l,ci,cs,mijST,mijDR;
int mij[NR][NR];
long long dp[NR][NR], S[NR], a[NR];
int main ()
{
f>>n;
for (i=1; i<=n; ++i) {
f>>a[i];
S[i]=S[i-1] + a[i];
dp[i][i]=a[i];
}
for (l=2; l<=n; ++l) { // lungimea
for (i=1; i<=n-l+1; ++i) { //capataul din stanga
ci=i; cs=i + l - 1;
if (ci==cs-1) { //daca e de lungime 2
dp[ci][cs]=2*(a[ci] + a[cs]);
mij[ci][cs]=ci;
} else {
mijST=mij[ci][cs-1];
mijDR=mij[ci+1][cs];
dp[ci][cs]=dp[ci][mijST] + dp[mijST+1][cs];
mij[ci][cs]=mijST;
for (j=mijST; j<=mijDR; ++j)
if (dp[ci][j] + dp[j+1][cs] < dp[ci][cs]) {
dp[ci][cs] =dp[ci][j] + dp[j+1][cs];
mij[ci][cs]=j;
}
dp[ci][cs]+=(S[cs] - S[ci-1]);
}
}
}
/*for (i=1; i<=n; ++i) {
for (j=1; j<=n; ++j)
g<<dp[i][j]<<" ";
g<<"\n";
}
g<<"\n";
for (i=1; i<=n; ++i)
g<<S[i]<<" ";
g<<"\n";*/
g<<dp[1][n]<<"\n";
return 0;
}