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

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个竞选者,每个选民有pi,ci,表示最初第i个人打算给pi投票,如果要改票给任意一个人需要ci花费。问要使1号竞选者的票比其他人都多最少需要多少花费。

题解:枚举第一个人最后的票数,则其他人都不能超过这个票数,然后将不投给1号的票按花费从大到小排序,花费较大的肯定不改票,贪心做一下即可。

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MX = 3e3 + 5;

struct node {
    int p, c;
    bool operator<(const node& _A)const {
        return c > _A.c;
    }
} a[MX];
int b[MX];
int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local

    int n, m; cin >> n >> m;
    rep(i, 1, n + 1) cin >> a[i].p >> a[i].c;
    rep(i, 1, n + 1) b[a[i].p]++;
    int cnt = 0; rep(i, 2, m + 1) if(b[i] >= b[1]) cnt++;
    if(!cnt) return printf("0\n") * 0;
    sort(a + 1, a + n + 1);
    ll ans = INFLL;
    rep(k, b[1], n + 1) {
        cnt = n - k;
        ll tmp = 0;
        memset(b, 0, sizeof(b));
        rep(i, 1, n + 1) if(a[i].p != 1 && cnt) {
            if(b[a[i].p] + 1 < k) b[a[i].p]++, cnt--;
            else tmp += a[i].c;
        } else if(a[i].p != 1) tmp += a[i].c;
        ans = min(ans, tmp);
    }
    cout << ans << endl;

#ifdef local
    cout << "time cost:" << clock() << "ms" << endl;
#endif // local
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值