import java
.util
.Scanner
;
public class DP7 {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
String s1
= sc
.nextLine();
int k
= Integer
.valueOf(s1
);
String s2
= sc
.nextLine();
String
[] str
= s2
.split(" ");
int[] prices
= new int[str
.length
];
for (int i
= 0; i
< str
.length
; i
++) {
prices
[i
]=Integer
.valueOf(str
[i
]);
}
DP7 dp7
= new DP7();
int res
= dp7
.dp(k
, prices
);
System
.out
.println(res
);
}
int dp(int K
,int[] prices
){
int N
= prices
.length
;
int[][][] dp
= new int[N
+1][K
+1][2];
for (int i
= 0; i
<K
+1 ; i
++) {
dp
[0][i
][0]=0;
dp
[0][i
][1]=(int)Float
.NEGATIVE_INFINITY
;
}
for (int i
= 0; i
<N
+1 ; i
++) {
dp
[i
][0][0]=0;
dp
[i
][0][1]=(int)Float
.NEGATIVE_INFINITY
;
}
for (int i
= 1; i
<N
+1 ; i
++) {
for (int j
= 1; j
<K
+1 ; j
++) {
dp
[i
][j
][0]=Math
.max(dp
[i
-1][j
][0],dp
[i
-1][j
][1]+prices
[i
-1]);
dp
[i
][j
][1]=Math
.max(dp
[i
-1][j
][1],dp
[i
-1][j
-1][0]-prices
[i
-1]);
}
}
return dp
[N
][K
][0];
}
}
转载请注明原文地址:https://tech.qufami.com/read-17714.html