C. Elections(枚举+贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1046765624/article/details/81876925

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

1 2
1 100

output

0

input

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

output

500

input

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

output

600

题目大概:

有n个投票人,m个候选人,票数最多的候选人获胜,你想要让1号候选人获胜,你需要买通其他投票人,使得他们投给1号票。每个人买通的费用不同,问最少可以多少钱,使得1号获胜。

思路:

1号获胜,一定是1号得到k个人投票,其他候选人都少于k个人投票。

在这里我们可以枚举这个k,贪心的计算1号获胜的最少花费。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=4000;
ll INF=1e18+7;
const int mod=1e9+7;
vector<pair <int,int> >a,G[maxn];
bool vis[maxn];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int w,id;
    int sum=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",&id,&w);
        if(id==1)sum++;
        else
        {
            a.push_back(make_pair(w,i));
            G[id].push_back(make_pair(w,i));
        }
    }
    sort(a.begin(),a.end());

    for(int i=1; i<=m; i++)sort(G[i].begin(),G[i].end());
    ll min_1=INF;

    for(int k=0; k<=n-1; k++)
    {
        memset(vis,0,sizeof(vis));
        ll sum1=0;
        int t=0;
        for(int i=2; i<=m; i++)
        {
            int p=0;
            while(G[i].size()-p>k)
            {
                sum1+=G[i][p].first;
                vis[G[i][p].second]=1;
                p++;
            }
            t+=p;
        }
        int ans=0;
        while(t+sum<=k)
        {
            if(!vis[a[ans].second])
            {
                sum1+=a[ans].first;
                t++;
                vis[a[ans].second]=1;
            }
            ans++;
        }
        min_1=min(min_1,sum1);
    }
    printf("%I64d\n",min_1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a1046765624/article/details/81876925