-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathN-Slimes.cpp
49 lines (39 loc) · 869 Bytes
/
N-Slimes.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
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define MAX 100001
#define MOD 1000000007
using namespace std;
int sum[401][401],cost[401][401];
void min_cost (int s,int e, int*a )
{
if(cost[s][e] != -1)
return;
if(s==e)
{
sum[s][e] = a[s];
cost[s][e] = 0;
return;
}
int minn = LLONG_MAX;
for (int i = s; i < e ; ++i)
{
min_cost (s,i,a);
min_cost (i+1,e,a);
sum[s][e] = sum[s][i] + sum[i+1][e];
minn = min ( minn , cost[s][i] + cost[i+1][e] + sum[s][e]);
}
cost[s][e] = minn;
}
int32_t main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i)
cin>>a[i];
memset(cost,-1, sizeof(cost));
min_cost (0,n-1,a);
cout<< cost [0][n-1] << endl;
return 0;
}