forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSumIncreasingSubsequence.java
More file actions
42 lines (33 loc) · 996 Bytes
/
MaximumSumIncreasingSubsequence.java
File metadata and controls
42 lines (33 loc) · 996 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
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
public class MaximumSumIncreasingSubsequence {
public static int MaximumSumIncreasingSubsequence(int[] A, int n) {
int max = 0;
int dp[] = new int[n];
for (int i = 0; i < n; i++) {
dp[i] = A[i];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (dp[i] < dp[j] + A[i] && A[i] > A[j]) {
dp[i] = dp[j] + A[i];
}
}
}
for (int i = 0; i < n; i++) {
if (dp[i] > max) {
max = dp[i];
}
}
return max;
}
public static void main(String[] args) {
//Dynamic Programming approach
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
}
System.out.println(MaximumSumIncreasingSubsequence(A, n));
}
}