As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.
Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th voter cici bytecoins you can ask him to vote for any other party you choose.
The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.
The first line of input contains two integers nn and mm (1≤n,m≤30001≤n,m≤3000) — the number of voters and the number of parties respectively.
Each of the following nn lines contains two integers pipi and cici (1≤pi≤m1≤pi≤m, 1≤ci≤1091≤ci≤109) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.
The United Party of Berland has the index 11.
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
1 2
1 100
0
5 5
2 100
3 200
4 300
5 400
5 900
500
5 5
2 100
3 200
4 300
5 800
5 900
600
In the first sample, The United Party wins the elections even without buying extra votes.
In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 33, 44 and 55 get one vote and party number 22 gets no votes.
In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdlib> 6 #include <cstring> 7 #include <string> 8 #include <deque> 9 using namespace std; 10 #define ll long long 11 #define gep(i,a,b) for(int i=a;i<=b;i++) 12 #define gepp(i,a,b) for(int i=a;i>=b;i--) 13 #define gep1(i,a,b) for(ll i=a;i<=b;i++) 14 #define gepp1(i,a,b) for(ll i=a;i>=b;i--) 15 #define N 3009 16 #define ph push_back 17 #define inf 10000000000000098 18 vector<int>ve[N]; 19 int n,m; 20 ll MIN; 21 ll solve(ll x){ 22 vector<int>se; 23 ll ret=0,cnt=ve[1].size();//初始值 24 gep(i,2,m){ 25 int num=ve[i].size(); 26 gep(j,0,num-1){ 27 if(num-j>=x){//严格大 28 ret+=ve[i][j];//必须加到party1上,因为加到别的上面,最后还是加到1上。 29 cnt++; 30 } 31 else{ 32 se.ph(ve[i][j]); 33 } 34 } 35 } 36 if(cnt<x){ 37 sort(se.begin(),se.end()); 38 for(int i=0;i<se.size()&&cnt<x;i++){//在从se里找 39 ret+=se[i]; 40 cnt++; 41 } 42 } 43 //cnt最后一定是>=x的 44 return ret; 45 } 46 int main() 47 { 48 int x; 49 ll y; 50 scanf("%d%d",&n,&m); 51 gep(i,1,n){ 52 scanf("%d%lld",&x,&y); 53 ve[x].ph(y); 54 } 55 gep(i,1,m) 56 sort(ve[i].begin(),ve[i].end()); 57 MIN=inf; 58 gep(i,1,n){//枚举查找,不能用二分 59 MIN=min(MIN,solve(i)); 60 } 61 printf("%lld\n",MIN); 62 return 0; 63 }