Guest User

Untitled

a guest
Jan 19th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define all(a) a.begin(),a.end()
  5. #define F first
  6. #define S second
  7. #define pb push_back
  8. #define ll long long
  9. #define vi vector<int>
  10. #define pi pair<int,int>
  11. #define mp make_pair
  12.  
  13. #ifdef LOCAL
  14. #include "debug.h"
  15. #else
  16. #define debug(...) 42
  17. #endif
  18.  
  19. const int mod=1e9+7;
  20.  
  21. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  22.  
  23. int mul(int a,int b)
  24. {
  25. return ((a)*1ll*(b))%mod;
  26. }
  27.  
  28. void add(int &a,int b)
  29. {
  30. a+=b;
  31. if(a>=mod)a-=mod;
  32. }
  33.  
  34. int sub(int a,int b){
  35. a-=b;
  36. if(a<0){
  37. a+=mod;
  38. }
  39. return a;
  40. }
  41.  
  42. int powz(int a,int b)
  43. {
  44. int res=1;
  45. while(b)
  46. {
  47. if(b&1){
  48. res=mul(res,a);
  49. }
  50. b/=2;
  51. a=mul(a,a);
  52. }
  53. return res;
  54. }
  55.  
  56. template <typename A, typename B>
  57. istream& operator>>(istream& input,pair<A,B>& x) {
  58. input>>x.F>>x.S;
  59. return input;
  60. }
  61.  
  62. template <typename A>
  63. istream& operator>>(istream& input,vector<A>& x) {
  64. for(auto& i:x)
  65. input>>i;
  66. return input;
  67. }
  68.  
  69. template<typename A>
  70. ostream& operator<<(ostream& output,vector<A>& x) {
  71. for(auto& i:x)
  72. output<<i<<' ';
  73. return output;
  74. }
  75.  
  76. const int N=1000002;
  77.  
  78. int dp[105][10005],dp2[105][10005],new_dp[105][10005];
  79.  
  80. void solve(){
  81. int n,l,r,k;
  82. cin>>n>>l>>r>>k;
  83. vi a(n);
  84. cin>>a;
  85. int tot=r-l+1;
  86. l--;r--;
  87. for(int i=0;i<l;i++){
  88. for(int j=0;j<=tot-1;j++){
  89. for(int put=j+1;put<=tot;put++){
  90. int tott=abs(put+l-1-i),val=a[l+put-1]-a[i];
  91. if(val<=0){
  92. continue;
  93. }
  94. for(int cost=0;cost<=k-tott;cost++){
  95. new_dp[put][cost+tott]=max(new_dp[put][cost+tott],dp[j][cost]+val);
  96. }
  97. }
  98. }
  99. for(int j=0;j<=tot;j++){
  100. for(int cost=0;cost<=k;cost++){
  101. dp[j][cost]=max(dp[j][cost],new_dp[j][cost]);
  102. new_dp[j][cost]=0;
  103. }
  104. }
  105. }
  106. for(int i=n-1;i>r;i--){
  107. for(int j=0;j<=tot-1;j++){
  108. for(int put=j+1;put<=tot;put++){
  109. int tott=abs(r-put+1-i),val=a[r-put+1]-a[i];
  110. if(val<=0){
  111. continue;
  112. }
  113. for(int cost=0;cost<=k-tott;cost++){
  114. new_dp[put][cost+tott]=max(new_dp[put][cost+tott],dp2[j][cost]+val);
  115. }
  116. }
  117. }
  118. for(int j=0;j<=tot;j++){
  119. for(int cost=0;cost<=k;cost++){
  120. dp2[j][cost]=max(dp2[j][cost],new_dp[j][cost]);
  121. new_dp[j][cost]=0;
  122. }
  123. }
  124. }
  125. vector<vector<int>>calc(n+1,vector<int>(10005));
  126. for(int j=0;j<=tot;j++){
  127. for(int cost=0;cost<=k;cost++){
  128. if(cost){
  129. calc[j][cost]=max({calc[j][cost],calc[j][cost-1],dp[j][cost]});
  130. }
  131. else{
  132. calc[j][cost]=dp[j][cost];
  133. }
  134. }
  135. }
  136. int ans=0;
  137. for(int i=0;i<=tot;i++){
  138. for(int j=0;j<=tot-i;j++){
  139. for(int cost=0;cost<=k;cost++){
  140. int cost2=k-cost;
  141. ans=max(ans,calc[i][cost2]+dp2[j][cost]);
  142. }
  143. }
  144. }
  145. int sum=ans;
  146. sum*=-1;
  147. for(int i=l;i<=r;i++){
  148. sum+=a[i];
  149. }
  150. cout<<sum;
  151. }
  152.  
  153. signed main(){
  154. ios_base::sync_with_stdio(false);
  155. cin.tie(0);
  156. int tc=1;
  157. //~cin>>tc;
  158. for(int _=0;_<tc;_++){
  159. //~ cout<<"Case #"<<_+1<<": ";
  160. solve();
  161. if(_!=tc-1)
  162. cout<<"\n";
  163. }
  164. }
Add Comment
Please, Sign In to add comment