Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- typedef pair<int, int> pi;
- typedef long long ll;
- int main()
- {
- int n, m; cin >> n >> m;
- vector<ll> one, two;
- for(int i =0; i < m; i++){
- int c;
- ll p; cin >> c >> p;
- if(c == 1){
- one.push_back(p);
- }else{
- two.push_back(p);
- }
- }
- sort(one.begin(), one.end());
- reverse(one.begin(), one.end());
- sort(two.begin(), two.end());
- reverse(two.begin(), two.end());
- ll ans = 0;
- int i = 0, j = 0; // i para one, j para two
- if(n & 1){
- if(two.size() > 0 && one.size() > 0){
- ll act = two[0] + one[0];
- ll nxt = 0;
- int co = 0;
- for(int x = 0; x <= 2 && x < one.size(); x++){
- nxt += one[x];
- co++;
- }
- if(nxt > act){
- i = min(3, (int)one.size());
- n -= co;
- ans += nxt;
- }else{
- i = 1;
- j = 1;
- ans += act;
- n -= 2;
- }
- }
- }
- // cout << ans << " " << n << " " << i << " " << j << endl;
- while(n > 0){
- bool taken = false;
- if(two.size() > j && n >= 2){
- ll act = two[j];
- int k;
- ll nxt = 0;
- int co = 0;
- for(k = i; k <= i + 1 && k < one.size(); k++){
- nxt += one[k];
- co++;
- }
- if(nxt > act){
- // los (dos primeros o uno solo) son mejor, que este solo de uno...
- ans += nxt;
- i = k;
- taken = true;
- n -= co;
- }else{
- ans += act;
- j++;
- taken = true;
- n -= 2;
- }
- }else{
- if(one.size() > i){
- ans += one[i];
- i++;
- taken = true;
- n--;
- }
- }
- if(!taken) break;
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement