fork download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. long long int dp[2][10003];
  7. long long int data[10003];
  8. int main() {
  9. // your code goes here
  10. int n,m;
  11. memset(dp,0,sizeof(dp));
  12. memset(data,0,sizeof(data));
  13. cin>>n>>m;
  14. for(int i=0;i<n;i++){
  15. cin>>data[i];
  16. }
  17.  
  18. for(int i=0;i<n;i++){
  19. for(int j=max(0,i-m+1);j<=i;j++){
  20. dp[0][i+m]=max(dp[0][i+m],dp[1][j]+data[i]);
  21. dp[1][j+m]=max(dp[1][j+m],dp[1][j]+data[i]);
  22. }
  23. for(int j=0;j<=i;j++){
  24. dp[1][i+1]=max(dp[1][i],dp[0][j]+data[i]);
  25. dp[0][i+m]=max(dp[0][i+m],dp[0][j]+data[i]);
  26. }
  27.  
  28.  
  29. for(int j=0;j<10;j++){
  30. cout<<j<<"("<<dp[0][j]<<" "<<dp[1][j]<<")";
  31. }
  32. cout<<endl;
  33. }
  34. long long int ans=0;
  35. for(int i=0;i<2*n+3;i++){
  36. for(int j=0;j<2;j++){
  37. ans=max(ans,dp[j][i]);
  38. }
  39. }
  40. cout<<ans<<endl;
  41. return 0;
  42. }
Success #stdin #stdout 0s 5288KB
stdin
6 3
3 7 1 5 6 4
stdout
0(0 0)1(0 3)2(0 0)3(3 3)4(0 0)5(0 0)6(0 0)7(0 0)8(0 0)9(0 0)
0(0 0)1(0 3)2(0 7)3(3 7)4(10 10)5(0 0)6(0 0)7(0 0)8(0 0)9(0 0)
0(0 0)1(0 3)2(0 7)3(3 7)4(10 10)5(8 8)6(0 0)7(0 0)8(0 0)9(0 0)
0(0 0)1(0 3)2(0 7)3(3 7)4(10 8)5(8 12)6(12 12)7(0 0)8(0 0)9(0 0)
0(0 0)1(0 3)2(0 7)3(3 7)4(10 8)5(8 16)6(12 13)7(16 14)8(0 0)9(0 0)
0(0 0)1(0 3)2(0 7)3(3 7)4(10 8)5(8 16)6(12 16)7(16 14)8(20 20)9(0 0)
20