fork 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. //? Approach 2 :
  132. int consistency4(int n, int m, int k){
  133.  
  134. vector<int> prevDp(n, 0);
  135. vector<int> curDp(n, 0);
  136.  
  137. for(int x = 1; x <= k; x++){
  138.  
  139. int sum = 0;
  140. for(int i = 0, j = 0; i < n; i++){
  141. sum += a[i];
  142.  
  143. if(i - j == m){
  144. sum -= a[j];
  145. j++;
  146. }
  147.  
  148. if(i-1 >= 0) curDp[i] = curDp[i-1];
  149.  
  150. if(i - j + 1 == m){
  151. if(j > 0)
  152. curDp[i] = max(curDp[i], prevDp[j-1] + sum);
  153. else
  154. curDp[i] = max(curDp[i], sum);
  155. }
  156. }
  157.  
  158. prevDp = curDp;
  159. }
  160.  
  161. return prevDp[n-1];
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. int practice(int n, int m, int k){
  175.  
  176.  
  177. return 0;
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184. void solve() {
  185.  
  186. int n, m, k;
  187. cin>> n >> m >> k;
  188.  
  189. a.resize(n);
  190. for(int i=0; i<n; i++) cin >> a[i];
  191.  
  192. // cout << consistency1(n, m, k) << " " << consistency2(n, m, k) << " " << consistency3(n, m, k) << endl;
  193. cout << consistency4(n, m, k) << endl;
  194.  
  195.  
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. int32_t main() {
  203. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  204.  
  205. int t = 1;
  206. // cin >> t;
  207. while (t--) {
  208. solve();
  209. }
  210.  
  211. return 0;
  212. }
Success #stdin #stdout 0s 5320KB
stdin
7 1 3
2 10 7 18 5 33 0
stdout
61