-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp11055.java
More file actions
42 lines (36 loc) · 1.11 KB
/
p11055.java
File metadata and controls
42 lines (36 loc) · 1.11 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
import java.io.*;
import java.util.StringTokenizer;
public class p11055 {
static int N, arr[];
static Integer dp[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N + 1];
dp = new Integer[N + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
dp[1] = arr[1];
for (int i = 1; i <= N; i++) {
recur(i);
}
int max = dp[1];
for (int i = 2; i <= N; i++) {
max = Math.max(max, recur(i));
}
System.out.println(max);
}
static int recur(int n) {
if (dp[n] == null) {
dp[n] = arr[n];
for (int i = n-1; i >= 0; i--) {
if (arr[i] < arr[n]) {
dp[n] = Math.max(dp[n], arr[n]+recur(i));
}
}
}
return dp[n];
}
}