Advertisement
mamelui

taxi

May 9th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef pair<int, int> pi;
  6. typedef long long ll;
  7.  
  8.  
  9. int main()
  10. {
  11.     int n, m; cin >> n >> m;
  12.  
  13.     vector<ll> one, two;
  14.  
  15.     for(int i =0; i < m; i++){
  16.         int c;
  17.         ll p; cin >> c >> p;
  18.         if(c == 1){
  19.             one.push_back(p);
  20.         }else{
  21.             two.push_back(p);
  22.         }
  23.     }
  24.  
  25.     sort(one.begin(), one.end());
  26.     reverse(one.begin(), one.end());
  27.  
  28.     sort(two.begin(), two.end());
  29.     reverse(two.begin(), two.end());
  30.  
  31.     ll ans = 0;
  32.     int i = 0, j = 0; // i para one, j para two
  33.  
  34.     if(n & 1){
  35.  
  36.         if(two.size() > 0 && one.size() > 0){
  37.             ll act = two[0] + one[0];
  38.             ll nxt = 0;
  39.             int co = 0;
  40.             for(int x = 0; x <= 2 && x < one.size(); x++){
  41.                 nxt += one[x];
  42.                 co++;
  43.             }
  44.  
  45.             if(nxt > act){
  46.                 i = min(3, (int)one.size());
  47.                 n -= co;
  48.                 ans += nxt;
  49.             }else{
  50.                 i = 1;
  51.                 j = 1;
  52.                 ans += act;
  53.                 n -= 2;
  54.             }
  55.         }
  56.     }
  57.    // cout << ans << " " << n << " " << i << " " << j << endl;
  58.     while(n > 0){
  59.         bool taken = false;
  60.  
  61.         if(two.size() > j && n >= 2){
  62.             ll act = two[j];
  63.             int k;
  64.             ll nxt = 0;
  65.             int co = 0;
  66.             for(k = i; k <= i + 1 && k < one.size(); k++){
  67.                 nxt += one[k];
  68.                 co++;
  69.             }
  70.  
  71.             if(nxt > act){
  72.                 // los (dos primeros o uno solo) son mejor, que este solo de uno...
  73.                 ans += nxt;
  74.                 i = k;
  75.                 taken = true;
  76.                 n -= co;
  77.  
  78.             }else{
  79.                 ans += act;
  80.                 j++;
  81.                 taken = true;
  82.                 n -= 2;
  83.             }
  84.         }else{
  85.             if(one.size() > i){
  86.                 ans += one[i];
  87.                 i++;
  88.                 taken = true;
  89.                 n--;
  90.             }
  91.         }
  92.  
  93.         if(!taken) break;
  94.     }
  95.     cout << ans << endl;
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement