Sunday 26 February 2012

Codeforces Round #109 (Div. 2)


                                                                  B. Combination
Ilya plays a card game by the following rules.

A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.

More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.

Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.

Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

SO I HAVE WRITTEN A SIMPLE CODE FOR THIS


#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<list>
#define show(x) copy(x.begin(),x.end(),output)
#include<iterator>
#define sort(x) sort(x.begin(),x.end())
#include<string>
#define vs vector<string>
#define vi vector<int>
#define pb push_back
#include<map>
#include<algorithm>
#include<sstream>
#include<stack>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<cctype>
using namespace std;
int main()
{
    multimap<int , int , less<int> > m;
    multimap<int,int>::iterator it ;
     multimap<int,int>::iterator iter;
    int n = 0;
    int a = 0;
    int b = 0;
    int i = 0 ;
    int score = 0;
    int count = 1;
    cin>>n;
    for(i = 0 ; i < n ; i ++)
    {
          cin>>a;
          cin>>b;
          m.insert(pair<int,int>(b,a));
         
    }
    while(count > 0)
    {
                count--;
                if(count >= 0)
                {
                         it = m.end();
                       
                         iter = ++it; // as iterator is invalidated
                         score =+ (iter->second);
                   count = count + (iter->first);
                   m.erase(iter);
                }  
    }
    cout<<score<<endl;
    return 0;
}  
BY : SAURABH BHATIA (GNDU , RC , JAL)

No comments:

Post a Comment