File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static int[][] dp;
9+ private static int[] file, sum;
10+ private static int K;
11+
12+ public static void main(String[] args) throws IOException {
13+ int T = Integer.parseInt(br.readLine());
14+ while (T-- > 0) {
15+ init();
16+ DP();
17+ bw.write(dp[1][K] + "\n");
18+ }
19+ bw.flush();
20+ bw.close();
21+ br.close();
22+
23+ }
24+
25+ private static void init() throws IOException {
26+ K = Integer.parseInt(br.readLine());
27+ StringTokenizer st = new StringTokenizer(br.readLine());
28+
29+ file = new int[K+1];
30+ sum = new int[K+1];
31+ dp = new int[K+1][K+1];
32+
33+ for (int i = 1; i <= K; i++) {
34+ file[i] = Integer.parseInt(st.nextToken());
35+ sum[i] = sum[i-1] + file[i];
36+ }
37+ }
38+
39+ private static void DP() {
40+ for (int i = 2; i <= K; i++) {
41+ for (int j = 1; j <= K-i+1; j++) {
42+ int k = i+j-1;
43+ dp[j][k] = Integer.MAX_VALUE;
44+
45+ for (int l = j; l < k; l++) {
46+ int cost = dp[j][l] + dp[l+1][k] + sum[k] - sum[j-1];
47+ dp[j][k] = Math.min(cost, dp[j][k]);
48+ }
49+ }
50+ }
51+ }
52+ }
53+ ```
You can’t perform that action at this time.
0 commit comments