Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <set>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. using namespace std;
  11.  
  12. int N, H, v[1002], t[1002];
  13. vector<vector<int> > g(1002);
  14.  
  15. int pd[1002][1002];
  16.  
  17. int func(int pos, int times){
  18.     if(pos == H+1){
  19.         return 0;
  20.     }else{
  21.         int &ans = pd[pos][times];
  22.         if(ans == -1){
  23.             ans = func(pos+1, times);
  24.             if(times < pos){
  25.                 int cur_time = times;
  26.                 int sum = 0;
  27.                 for(int j = 0; j < g[pos].size() && cur_time < pos; j++){
  28.                     sum += g[pos][j];
  29.                     cur_time++;
  30.                     ans = max(ans, sum + func(pos+1, cur_time));
  31.                 }
  32.  
  33.             }
  34.         }
  35.         return ans;
  36.     }
  37. }
  38.  
  39. int main(void){
  40.     freopen("in.in", "r", stdin);
  41.     while(cin >> N >> H){
  42.         for(int i = 0; i <= H; i++){
  43.             g[i].clear();
  44.         }
  45.         int sum = 0;
  46.         for(int i = 0; i < N; i++){
  47.             cin >> v[i] >> t[i];
  48.             g[t[i]].push_back(v[i]);
  49.             sum += v[i];
  50.         }
  51.         for(int i = 1; i <= H; i++){
  52.             sort(g[i].begin(), g[i].end(), greater<int>() );
  53.         }
  54.         memset(pd, -1, sizeof(pd));
  55.         cout << sum - func(0, 0) << endl;
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement