Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define ll long long
- #define N ((int)6e4 + 5)
- #define MOD ((int)1e9 + 7)
- #define MAX ((int)1e9 + 7)
- #define MAXL ((ll)1e18 + 7)
- #define MAXP ((int)1e3 + 7)
- #define thr 1e-8
- #define pi acos(-1) /// pi = acos ( -1 )
- #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
- #define endl "\n"
- using namespace std;
- /// iterative dp
- LL solve(int cur , int rem_pro){
- if(rem_pro < 0) return -MAX;
- if(cur == 0){
- if(rem_pro == 0) return 0;
- else return -MAX;
- }
- if(dpp[cur][rem_pro] != -1) return dpp[cur][rem_pro];
- LL ans = solve(cur - 1, rem_pro);
- ans = max(ans , solve(cur - 1 , rem_pro - 1) + a[cur]);
- if(ans < 0) ans = -MAX;
- return dpp[cur][rem_pro] = ans;
- }
- int main()
- {
- /// problem: https://codeforces.com/problemset/problem/1526/C1
- int n;
- cin>>n;
- for(int profit = 0 ; profit <= n ; profit++){
- if(solve(n , profit) >= 0) ans = profit;
- }
- ///iterative dp
- for(int rem_pro = 1 ; rem_pro <= n ; rem_pro++) dpp[0][rem_pro] = -MAX;
- dpp[0][0] = 0;
- for(int cur = 1 ; cur <= n ; cur++){ /// cur = 4 -> 5
- for(int rem_pro = 0 ; rem_pro <= n ; rem_pro++){
- LL ans = dpp[cur - 1][rem_pro];
- if(rem_pro - 1 >= 0) ans = max(ans , dpp[cur - 1][rem_pro - 1] + a[cur]);
- if(ans < 0) ans = -MAX;
- dpp[cur][rem_pro] = ans;
- }
- }
- for(int profit = 0 ; profit <= n ; profit++){
- if(dpp[n][profit] >= 0) ans = profit;
- }
- cout<<ans<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment