[Link] https://www.acmicpc.net/problem/11066
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
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
String[] line;
StringBuilder sb = new StringBuilder();
int t = toi(br.readLine());
for(int iter = 0; iter < t; iter++) {
int k = toi(br.readLine());
int[][] dp = new int[k][k];
int[] arr = Arrays.stream(getLine()).mapToInt(Integer::parseInt).toArray();
int[][] sum = new int[k][k];
for(int i = 0; i < k; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
dp[i][i] = sum[i][i] = arr[i];
}
for(int di = 1; di < k; di++)
for(int i = 0; i < k - di; i++)
sum[i][i + di] = sum[i][i + di - 1] + arr[i + di];
for(int i = 1; i < k; i++) { // 1 ~ k - 1- i (y - x)
for(int l = 0; l < k - i; l++) {
for(int mid = l; mid < l + i; mid++) {
if(i == k -1) dp[l][l + i] = Math.min(dp[l][l + i], dp[l][mid] + dp[mid + 1][l + i]);
else dp[l][l + i] = Math.min(dp[l][l + i], dp[l][mid] + dp[mid + 1][l + i] + sum[l][l + i]);
}
}
}
sb.append(dp[0][k-1] + "\n");
}
print(sb);
}
static int sum(int[] arr, int s, int e) { int sum = 0; for(int i = s; i <= e; i++) sum+=arr[i]; return sum; }
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static <T> void print(T s) { System.out.print(s); }
}