-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircular_Merging.cpp
More file actions
60 lines (46 loc) · 1.21 KB
/
Circular_Merging.cpp
File metadata and controls
60 lines (46 loc) · 1.21 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
// https://www.codechef.com/problems/CIRMERGE
// https://discuss.codechef.com/t/cirmerge-editorial/29723
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f3f3f3f3f
ll a[405], dp[405][405], sum[405][405];
int n;
ll minPenalty(int i, int j)
{
if(dp[i][j] != -1)
return dp[i][j];
if(i == j)
return dp[i][j] = 0;
if((i+1) % n == j)
return dp[i][j] = a[i] + a[j];
ll ret = INF;
for(int k = i; k != j; k = (k+1)%n)
ret = min(ret, minPenalty(i,k) + minPenalty((k+1)%n, j) + sum[i][j]);
return dp[i][j] = ret;
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n;
memset(a, -1, sizeof(a));
memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; i++)
cin >> a[i];
ll ans = INF;
//pre-compute sum from arr[i...j] - (both inclusive)
for(int i = 0; i < n; i++)
{
sum[i][i] = a[i];
for(int j = (i+1)%n; j != i; j = (j+1)%n)
sum[i][j] = sum[i][(j+n-1)%n] + a[j];
}
for(int i = 0; i < n; i++)
ans = min(ans, minPenalty(i, (i+n-1) % n));
cout << ans << endl;
}
return 0;
}