a53

best_sum2

a53
Jan 12th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <deque>
  6. using namespace std;
  7.  
  8. long long d[4501];
  9. long long dp[301][1501];
  10.  
  11. deque <long long> Q;
  12.  
  13. struct kkk{
  14. long long max;
  15. int poz;
  16. } P[3];
  17.  
  18. int main() {
  19. int n, k, t;
  20.  
  21. cin >> n >> t >> k;
  22. if(n <= 1500 && t <= 300){
  23.  
  24. for(int i = 1; i <= n; ++i)
  25. cin >> dp[1][i];
  26.  
  27. for(int i = 2; i <= t; ++i)
  28. for(int j = i; j <= n; ++j)
  29. dp[i][j] = -1000000000000000000;
  30.  
  31. for(int i = 2; i <= t; ++i)
  32. for(int j = i; j <= n; ++j)
  33. for(int f = j - 1; f >= j - k && f >= i - 1; --f)
  34. dp[i][j] = max(dp[i][j], dp[i - 1][f] + dp[1][j]);
  35.  
  36. long long maxi = -1000000000000000000;
  37.  
  38. for(int i = t; i <= n; ++i)
  39. maxi = max(maxi, dp[t][i]);
  40.  
  41. cout << maxi;
  42.  
  43. return 0;
  44. }
  45. P[1].max = -1000000000000000000;
  46.  
  47. for(int i = 1; i <= n; ++i) {
  48. cin >> d[i];
  49.  
  50. if(d[i] > P[2].max){
  51. P[2].max = d[i];
  52. P[2].poz = i;
  53. }
  54. if(d[i] > P[1].max){
  55. P[1] = {d[i], i};
  56. P[2].max = -1000000000000000000;
  57. }
  58. else
  59. if( i - k >= P[1].poz) {
  60. P[1] = P[2];
  61. P[2].max = -1000000000000000000;
  62. }
  63.  
  64. Q.push_front(P[1].max);
  65. }
  66.  
  67.  
  68. for(int i = 2; i <= t; ++i) {
  69. P[1].max = -1000000000000000000;
  70. Q.pop_front();
  71.  
  72. for(int j = i; j <= n; ++j) {
  73. long long x = d[j] + Q.back();
  74.  
  75. if(x > P[2].max){
  76. P[2].max = x;
  77. P[2].poz = j;
  78. }
  79.  
  80. if(x > P[1].max){
  81. P[1] = {x, j};
  82. P[2].max = -1000000000000000000;
  83. }
  84. else
  85. if( j - k >= P[1].poz) {
  86. P[1] = P[2];
  87. P[2].max = -1000000000000000000;
  88. }
  89.  
  90.  
  91. Q.pop_back();
  92.  
  93. Q.push_front(P[1].max);
  94. }
  95. }
  96. long long maxi = -1000000000000000000;
  97.  
  98. while(Q.size()){
  99. if(Q.back() > maxi)
  100. maxi = Q.back();
  101. Q.pop_back();
  102. }
  103.  
  104. cout << maxi;
  105.  
  106. return 0;
  107. }
Add Comment
Please, Sign In to add comment