Advertisement
Guest User

CCC15S5 Greedy For Pies

a guest
Oct 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int N, M;
  6. int ogpies[3005], insert[105];
  7. int dp[3005][2][105][105];
  8.  
  9.  
  10. int doo(int curr, int take, int l , int r){
  11.     //cout << curr << "\n";
  12.     int& temp = dp[curr][take][l][r];
  13.  
  14.     if (temp != -1){
  15.         return temp;
  16.     }
  17.     if(curr == N+1) {
  18.         if (l <= r) {
  19.             if (take == 1) {
  20.                 temp = insert[r] + doo(curr, 0, l, r - 1);
  21.                 return temp;
  22.             } else if (take == 0) {
  23.                 temp = doo(curr, 1, l + 1, r);
  24.                 return temp;
  25.             }
  26.         }
  27.         else {
  28.             temp = 0;
  29.             return temp;
  30.  
  31.         }
  32.     }
  33.     else if(take == 1){
  34.         temp = max(doo(curr, 0, l, r), ogpies[curr] + doo(curr+1, 0, l, r));
  35.         if (l <= r){
  36.             temp = max(temp, insert[r] + doo(curr, 0, l, r-1));
  37.         }
  38.     }
  39.     else{
  40.         temp = doo(curr+1, 1, l, r);
  41.         if (l <= r){
  42.             temp = max(temp, doo(curr, 1, l+1, r));
  43.         }
  44.     }
  45.     return temp;
  46. }
  47.  
  48.  
  49. int main() {
  50.     cin.sync_with_stdio(0);
  51.     cin.tie(0);
  52.     cin >> N;
  53.     for (int i = 1; i <= N; i++){
  54.         int t; cin >> t;
  55.         ogpies[i] = t;
  56.     }
  57.     cin >> M;
  58.     for (int i = 1; i <= M; i++){
  59.         int t; cin >> t;
  60.         insert[i] = t;
  61.     }
  62.  
  63.     memset(dp, -1, sizeof dp);
  64.     sort(insert+1, insert+1+M);
  65.  
  66.     cout << doo(1, 1, 1, M);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement