C. Elections

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 — n

and m 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 i-th voter ci

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.

Input

The first line of input contains two integers n

and m (1≤n,m≤3000

) — the number of voters and the number of parties respectively.

Each of the following n

lines contains two integers pi and ci (1≤pi≤m, 1≤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 1

.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples

Input

Copy

1 2
1 100

Output

Copy

0

Input

Copy

5 5
2 100
3 200
4 300
5 400
5 900

Output

Copy

500

Input

Copy

5 5
2 100
3 200
4 300
5 800
5 900

Output

Copy

600

Note

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 3

, 4 and 5 get one vote and party number 2

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.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)


const int maxn=3010;
struct GG{

     int x,cnt;
     bool operator<(const GG& obj)const{
        return cnt>obj.cnt;
    }
}p[maxn];

int vis[maxn][maxn];
ll group[maxn][maxn];
ll sum[maxn][maxn];

ll rest[maxn];
int n,m;
/*
这题真好,就是不会 当时想过枚举,
但是想到了一种情况  最终的票数 可以是把其他高的票数拿下来,或者是从低的票数中拿
当时:
最终的票数有多种选择---> 需要知道达到这个票数下最少的钱数,并且票数最高---> 两种选择(高、低),继而N种情况,GG放弃

做法:
贪心:如果知道最终的票数x,那么比这个票数高的都得小于x,然后再从剩下的票数中选出来 y张价值最小的票
问题也就是从有高有低,都变成了比x小的,把大于x的直接全部砍掉高的部分,剩下的直接贪心排序就可以了
如果砍不完,就是票数是在太低,不可能赢,所以继续升高

票数答案一定在  [最小值就是当前票数+1,最大值就是max(票)+1],不存在单调性,只能枚举
*/


int main(){
    scanf("%d %d",&n,&m);
	rep(i,1,n+1){
	    int g;ll c;
        scanf("%d %lld",&g,&c);
        p[g].cnt++;
        group[g][0]++;
        group[g][group[g][0]]=c;
	}

	int wrong=0;
	rep(i,2,m+1){
	    p[i].x=i;
        sort(group[i]+1,group[i]+1+group[i][0]);
        rep(j,1,group[i][0]+1)sum[i][j]=sum[i][j-1]+group[i][j];//偏移一位
        if(group[i][0]>=group[1][0])wrong=max(wrong,(int)group[i][0]);
	}


	if(!wrong){
        printf("0\n");
        return 0;
	}

	sort(p+2,p+m+1);

    ll ans=1e18;
    
    rep(i,group[1][0]+1,wrong+2){
        int cnt=group[1][0];ll s=0;
        rep(j,2,n+1){
            if(p[j].cnt>=i){
                int num=(p[j].cnt-(i-1));
                s+=sum[p[j].x][num];
                cnt+=num;
                rep(k,1,num+1)vis[p[j].x][k]=i;
            }
        }
        if(cnt>i)continue;
        int num=0;
        rep(j,2,m+1){
            rep(k,1,group[j][0]+1)if(vis[j][k]!=i)rest[num++]=group[j][k];
        }
        sort(rest,rest+num);
        rep(j,0,i-cnt)s+=rest[j];
        //rep(j,0,num)printf("j:%d %lld\n",j,rest[j]);
        //printf("i:%d cnt:%d num:%d s:%lld\n",i,cnt,num,s);
        ans=min(ans,s);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81605808