-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11066.cpp
More file actions
35 lines (30 loc) · 791 Bytes
/
11066.cpp
File metadata and controls
35 lines (30 loc) · 791 Bytes
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
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <algorithm>
using namespace std;
int dp[501][501];
int cost[501];
int sum[501];
int t, k, i;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &k);
for (i = 1; i <= k; ++i) {
scanf("%d", &cost[i]);
sum[i] = sum[i - 1] + cost[i];
}
for (int d = 1; d < k; ++d) {
for (int tx = 1; tx + d <= k; ++tx) {
int ty = tx + d;
dp[tx][ty] = INT_MAX;
for (int mid = tx; mid < ty; ++mid)
dp[tx][ty] =
min(dp[tx][ty], dp[tx][mid] + dp[mid + 1][ty] + sum[ty] - sum[tx - 1]);
}
}
printf("%d\n", dp[1][k]);
}
return 0;
}