_no0B

Untitled

Nov 21st, 2021
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define N ((int)6e4 + 5)
  4. #define MOD ((int)1e9 + 7)
  5. #define MAX ((int)1e9 + 7)
  6. #define MAXL ((ll)1e18 + 7)
  7. #define MAXP ((int)1e3 + 7)
  8. #define thr 1e-8
  9. #define pi acos(-1)  /// pi = acos ( -1 )
  10. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  11. #define endl "\n"
  12.  
  13. using namespace std;
  14.  
  15. /// iterative dp
  16.  
  17. LL solve(int cur , int rem_pro){
  18.     if(rem_pro < 0) return -MAX;
  19.     if(cur == 0){
  20.         if(rem_pro == 0) return 0;
  21.         else return -MAX;
  22.     }
  23.     if(dpp[cur][rem_pro] != -1) return dpp[cur][rem_pro];
  24.     LL ans = solve(cur - 1, rem_pro);
  25.     ans = max(ans , solve(cur - 1 , rem_pro - 1) + a[cur]);
  26.     if(ans < 0) ans = -MAX;
  27.     return dpp[cur][rem_pro] = ans;
  28. }
  29.  
  30. int main()
  31. {
  32.     /// problem: https://codeforces.com/problemset/problem/1526/C1
  33.     int n;
  34.     cin>>n;
  35.     for(int profit = 0 ; profit <= n ; profit++){
  36.         if(solve(n , profit) >= 0) ans = profit;
  37.     }
  38.  
  39.     ///iterative dp
  40.     for(int rem_pro = 1 ; rem_pro <= n ; rem_pro++) dpp[0][rem_pro] = -MAX;
  41.     dpp[0][0] = 0;
  42.  
  43.  
  44.     for(int cur = 1 ; cur <= n ; cur++){ /// cur = 4 -> 5
  45.         for(int rem_pro = 0 ; rem_pro <= n ; rem_pro++){
  46.             LL ans = dpp[cur - 1][rem_pro];
  47.             if(rem_pro - 1 >= 0) ans = max(ans , dpp[cur - 1][rem_pro - 1] + a[cur]);
  48.             if(ans < 0) ans = -MAX;
  49.             dpp[cur][rem_pro] = ans;
  50.         }
  51.     }
  52.  
  53.     for(int profit = 0 ; profit <= n ; profit++){
  54.         if(dpp[n][profit] >= 0) ans = profit;
  55.     }
  56.  
  57.  
  58.  
  59.     cout<<ans<<endl;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment