- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlimes.java
More file actions
Latest commit
29 lines (24 loc) · 777 Bytes
/
Copy pathSlimes.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
importjava.util.*;
importjava.io.*;
publicclassSlimes {
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
intn = Integer.parseInt(br.readLine());
String[] s = br.readLine().split(" ");
long[] prefix = newlong[n + 1];
for (inti = 0; i < n; i++)
prefix[i + 1] = prefix[i] + Integer.parseInt(s[i]);
long[][] dp = newlong[n + 1][n + 1];
for (inti = 2; i <= n; i++) {
for (intj = i - 1; j > 0; j--) {
dp[j][i] = Long.MAX_VALUE;
for (intk = j; k < i; k++) {
longval = 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]);
}
}