-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11066.cpp
More file actions
68 lines (56 loc) · 1.12 KB
/
11066.cpp
File metadata and controls
68 lines (56 loc) · 1.12 KB
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
#include <stdio.h>
#include <vector>
#define INF 987654321
using namespace std;
typedef struct MERGED
{
int size, acc;
} MERGED;
vector<vector<MERGED>> dp;
vector<int> files;
MERGED merge(int left, int right)
{
MERGED re;
re.acc = INF;
if (dp[left][right].acc != INF)
{
re = dp[left][right];
}
else if (left == right)
{
re = {files[left], 0};
}
else
{
for (int i = left; i < right; i++)
{
MERGED L = merge(left, i);
MERGED R = merge(i + 1, right);
MERGED tmp;
tmp.size = L.size + R.size;
tmp.acc = L.acc + R.acc + tmp.size;
if (re.acc > tmp.acc)
{
re = tmp;
}
}
}
return dp[left][right] = re;
}
int main()
{
int T, K;
scanf("%d", &T);
while (T--)
{
scanf("%d", &K);
files.assign(K, 0);
dp.assign(K, vector<MERGED>(K, {INF, INF}));
for (int i = 0; i < K; i++)
{
scanf("%d", &files[i]);
}
printf("%d\n", merge(0, K - 1).acc);
}
return 0;
}