File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int N = Integer . parseInt(br. readLine());
9+ int [] arr = new int [N + 1 ];
10+ StringTokenizer st = new StringTokenizer (br. readLine());
11+ for (int i = 1 ; i <= N ; i++ ) {
12+ arr[i] = Integer . parseInt(st. nextToken());
13+ }
14+ int [] dp = new int [N + 1 ];
15+ dp[0 ] = 0 ;
16+
17+ for (int i = 1 ; i <= N ; i++ ) {
18+ dp[i] = 0 ;
19+ int mx = arr[i];
20+ int mn = arr[i];
21+ for (int j = i; j >= 1 ; j-- ) {
22+ mx = Math . max(mx, arr[j]);
23+ mn = Math . min(mn, arr[j]);
24+ dp[i] = Math . max(dp[i], dp[j- 1 ] + (mx- mn));
25+ }
26+ }
27+
28+ System . out. println(dp[N ]);
29+ }
30+ }
31+
32+ ```
You can’t perform that action at this time.
0 commit comments