(贪心)DIV1 codeforces—1019A.Elections

A. Elections

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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.

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 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.

题意:现有 n 个选民,m 个政党,1 号政党是我们支持的政党,这些选民是可以用钱收买的,问 为了使我们的政党被选上(票数             比任何一个政党票数都多),最少需要花费多少钱?

题解:见代码

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#define e 0.000001
using namespace std;
typedef long long ll;
map<int,int>mapp;
struct fun{
    int id;  // 政党编号
    ll s;   //  收买选民的价钱
}f[3005];
bool cmp(fun x,fun y){
    return x.s<y.s;
}
int per[3005];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d %lld",&f[i].id,&f[i].s);
        mapp[f[i].id]++;
    }
    sort(f+1,f+n+1,cmp);
    ll ans=9999999999999;
    for(int i=mapp[1];i<=n;i++){  // 枚举获胜的票数状态
        memset(per,0,sizeof(per));  // per 数组保存的是 1 号政党相对于 i 政党要再收买 per[i] 的票才能获胜
        int sum=0;  // sum 保存的是 1 号政党相对于 所以 政党要再收买 sum 的票才能获胜
        ll cnt=0;   // 需要支付多少钱
        for(int j=2;j<=m;j++){
            if(mapp[j]>=i){
                per[j]=mapp[j]-i+1;
                sum+=per[j];
            }
        }
        if(i-mapp[1]<sum)  // 如果当前这种状态不能获胜,则丢弃
            continue;
        int k=i-mapp[1]-sum;  // 剩余的票从其他比 1 号政党票少但价钱便宜的里边扣除
        for(int i=1;i<=n;i++){
            if(per[f[i].id]){
                per[f[i].id]--;
                cnt+=f[i].s;
            }
            else{
                if(k!=0&&f[i].id!=1){
                    cnt+=f[i].s;
                    k--;
                }
            }
        }
        ans=min(ans,cnt);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/81672818