日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 1020C】Elections (枚举投票数,贪心)

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 1020C】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?—?nnand?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

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

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個黨派,第i個人開始想把票投給黨派pi,而如果想讓他改變他的想法需要花費ci元。你現在是黨派1,問你最少花多少錢使得你的黨派得票數大于其它任意黨派。

解題報告:

? ? 一般思路想到就是二分+check,不難發現這題是沒有單調性的,而且時間復雜度允許,我們就從小到大枚舉得票數,維護一個最小話費就行了

AC代碼:
?

#include<bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 10000 + 5; vector<ll > a[MAXN]; ll cnt[MAXN],bk[MAXN]; pair<ll , ll> p[MAXN]; int main() {int n,m;cin>>m>>n;for(int j = 0; j<n; j++) {scanf("%lld%lld",&p[j].second,&p[j].first);p[j].second--;cnt[p[j].second]++;}sort(p,p+n);ll ans = LLONG_MAX;for(int v = 1; v <=n; v++) {memset(cnt,0,sizeof(cnt));memset(bk,0,sizeof(bk));for(int j = 0; j<n; j++) cnt[p[j].second]++;ll chu = cnt[0],cost = 0;for(int i = 0; i<n; i++) {if(p[i].second !=0 && cnt[p[i].second ] >= v) {cost += p[i].first;chu++;cnt[p[i].second]--;bk[i] = 1;}}for(int i = 0; i<n; i++) {if(p[i].second !=0 && chu < v && bk[i] == 0) {chu++;cost += p[i].first;}}ans = min(ans,cost);}printf("%lld\n",ans);return 0 ; }

?

總結

以上是生活随笔為你收集整理的【CodeForces - 1020C】Elections (枚举投票数,贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。