Advertisement
Guest User

Untitled

a guest
Apr 21st, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. long long A[2000009];
  3. long long dp1[2000009],dp2[2000009],cnt1[2000009],cnt2[2000009];
  4. using namespace std;
  5. int main()
  6. {
  7. long long N,M,K,j,k,low,high,mid,from,to;
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(0);
  10. cin >> N >> K;
  11. for(register int i = 1;i <= N;i++)
  12. {
  13. cin >> A[i];
  14. }
  15. low = -1;
  16. high = 50000;
  17. while(low + 1 < high)
  18. {
  19. mid = (low + high)/2;
  20. /*for(register int i = 0;i <= N;i++)
  21. {
  22. dp1[i] = 0;
  23. dp2[i] = -1000000000000000000;
  24. cnt1[i] = 0;
  25. cnt2[i] = 0;
  26. }*/
  27. memset(dp1,0,N+1);
  28. memset(dp2,-2000000000,N+1);
  29. memset(cnt1,0,N+1);
  30. memset(cnt2,0,N+1);
  31. for(register int i = 1;i <= N;i++)
  32. {
  33. dp1[i] = dp1[i-1];
  34. cnt1[i] = cnt1[i-1];
  35. if(dp2[i-1] + A[i] - mid > dp1[i])
  36. {
  37. dp1[i] = dp2[i-1] + A[i] - mid;
  38. cnt1[i] = cnt2[i-1] + 1;
  39. }
  40. dp2[i] = dp2[i-1];
  41. cnt2[i] = cnt2[i-1];
  42. if(dp1[i-1] - A[i] > dp2[i])
  43. {
  44. dp2[i] = dp1[i-1] - A[i];
  45. cnt2[i] = cnt1[i-1];
  46. }
  47. }
  48. if(cnt1[N] > K)
  49. {
  50. low = mid;
  51. }
  52. else
  53. {
  54. high = mid;
  55. }
  56. }
  57. //cout << "high = " << high <<endl;
  58. //cout << "cnt1[N] = " << cnt1[N] <<endl;
  59. mid = high;
  60. for(register int i = 0;i <= N;i++)
  61. {
  62. dp1[i] = 0;
  63. dp2[i] = -1000000000000000000;
  64. cnt1[i] = 0;
  65. cnt2[i] = 0;
  66. }
  67. for(register int i = 1;i <= N;i++)
  68. {
  69. dp1[i] = dp1[i-1];
  70. cnt1[i] = cnt1[i-1];
  71. if(dp2[i-1] + A[i] - mid > dp1[i])
  72. {
  73. dp1[i] = dp2[i-1] + A[i] - mid;
  74. cnt1[i] = cnt2[i-1] + 1;
  75. }
  76. dp2[i] = dp2[i-1];
  77. cnt2[i] = cnt2[i-1];
  78. if(dp1[i-1] - A[i] > dp2[i])
  79. {
  80. dp2[i] = dp1[i-1] - A[i];
  81. cnt2[i] = cnt1[i-1];
  82. }
  83. }/*
  84. for(i = 1;i <= N;i++)
  85. {
  86. cout << dp1[i] <<" ";
  87. }
  88. cout <<endl;
  89. for(i = 1;i <= N;i++)
  90. {
  91. cout << dp2[i] <<" ";
  92. }
  93. cout <<endl;
  94. for(i = 1;i <= N;i++)
  95. {
  96. cout << cnt1[i] <<" ";
  97. }
  98. cout <<endl;
  99. for(i = 1;i <= N;i++)
  100. {
  101. cout << cnt2[i] <<" ";
  102. }
  103. cout <<endl;*/
  104. //assert(cnt1[N] <= K);
  105. cout << max(0LL,dp1[N]+K*mid) <<endl;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement