fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. int consistency1(int n, int m, int k){
  33.  
  34. vector<vector<int>> dp(n+1, vector<int>(k+1, -INF));
  35. dp[0][0] = 0;
  36.  
  37. for(int i=m; i<=n; i++){
  38.  
  39. for(int x=1; x<=k; x++){
  40.  
  41. int sum = 0;
  42. int j = i;
  43. while(j>=i-m+1){
  44. sum += a[j-1];
  45. j--;
  46. }
  47.  
  48. while(j>=0){
  49. dp[i][x] = max(dp[i][x] , dp[j][x-1] + sum );
  50. j--;
  51. }
  52. }
  53. }
  54.  
  55. int maxi = -INF;
  56. for(int i=1; i<=n; i++){
  57. maxi = max(maxi, dp[i][k]);
  58. }
  59.  
  60. return maxi;
  61. }
  62.  
  63.  
  64.  
  65.  
  66. int consistency2(int n, int m, int k){
  67.  
  68. vector<vector<int>> dp(n+1, vector<int>(k+1, -INF));
  69. vector<vector<int>> pMaxi(n+1, vector<int>(k+1, 0));
  70. dp[0][0] = 0;
  71.  
  72. for(int x=1; x<=k; x++){
  73.  
  74. int sum = 0;
  75. for(int i=1, j=1; i<=n; i++){
  76. sum += a[i-1];
  77.  
  78. if(i-j == m){
  79. sum -= a[j-1];
  80. j++;
  81. }
  82.  
  83. if(i-j+1 == m){
  84. dp[i][x] = pMaxi[j-1][x-1] + sum;
  85. pMaxi[i][x] = max(pMaxi[i-1][x], dp[i][x]);
  86. }
  87. }
  88. }
  89.  
  90.  
  91. return pMaxi[n][k];
  92. }
  93.  
  94.  
  95.  
  96.  
  97. //* Space optimized
  98.  
  99. int consistency3(int n, int m, int k){
  100.  
  101. vector<int> prevPMaxi(n+1, 0);
  102. vector<int> curPMaxi(n+1, 0);
  103.  
  104. for(int x=1; x<=k; x++){
  105.  
  106. int sum = 0;
  107. for(int i=1, j=1; i<=n; i++){
  108. sum += a[i-1];
  109.  
  110. if(i-j == m){
  111. sum -= a[j-1];
  112. j++;
  113. }
  114.  
  115. if(i-j+1 == m){
  116. int dp = prevPMaxi[j-1] + sum;
  117. curPMaxi[i] = max(curPMaxi[i-1], dp);
  118. }
  119. }
  120.  
  121. prevPMaxi = curPMaxi;
  122. }
  123.  
  124.  
  125. return prevPMaxi[n];
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. int practice(int n, int m, int k){
  144.  
  145.  
  146. return 0;
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. void solve() {
  154.  
  155. int n, m, k;
  156. cin>> n >> m >> k;
  157.  
  158. a.resize(n);
  159. for(int i=0; i<n; i++) cin >> a[i];
  160.  
  161. // cout << consistency1(n, m, k) << " " << consistency2(n, m, k) << " " << consistency3(n, m, k) << endl;
  162. cout << consistency3(n, m, k) << endl;
  163.  
  164.  
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171. int32_t main() {
  172. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  173.  
  174. int t = 1;
  175. // cin >> t;
  176. while (t--) {
  177. solve();
  178. }
  179.  
  180. return 0;
  181. }
Success #stdin #stdout 0s 5304KB
stdin
7 1 3
2 10 7 18 5 33 0
stdout
61