-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlimes.java
More file actions
29 lines (24 loc) · 777 Bytes
/
Slimes.java
File metadata and controls
29 lines (24 loc) · 777 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
import java.util.*;
import java.io.*;
public class Slimes {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
long[] prefix = new long[n + 1];
for (int i = 0; i < n; i++)
prefix[i + 1] = prefix[i] + Integer.parseInt(s[i]);
long[][] dp = new long[n + 1][n + 1];
for (int i = 2; i <= n; i++) {
for (int j = i - 1; j > 0; j--) {
dp[j][i] = Long.MAX_VALUE;
for (int k = j; k < i; k++) {
long val = dp[j][k] + dp[k + 1][i] + (prefix[k] - prefix[j - 1]) + (prefix[i] - prefix[k]);
if (dp[j][i] > val)
dp[j][i] = val;
}
}
}
System.out.println(dp[1][n]);
}
}