Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A multi-subject competition is coming! The competition has mm different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students.

He has nn candidates. For the ii-th person he knows subject sisi the candidate specializes in and riri — a skill level in his specialization (this level can be negative!).

The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.

Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.

(Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).

Input

The first line contains two integers nn and mm (1n1051≤n≤105, 1m1051≤m≤105) — the number of candidates and the number of subjects.

The next nn lines contains two integers per line: sisi and riri (1sim1≤si≤m, 104ri104−104≤ri≤104) — the subject of specialization and the skill level of the ii-th candidate.

Output

Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or 00 if every valid non-empty delegation has negative sum.

Examples
input
Copy
6 3
2 6
3 6
2 5
3 5
1 9
3 1
output
Copy
22
input
Copy
5 3
2 6
3 6
2 5
3 5
1 11
output
Copy
23
input
Copy
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
output
Copy
0
Note

In the first example it's optimal to choose candidates 11, 22, 33, 44, so two of them specialize in the 22-nd subject and other two in the 33-rd. The total sum is 6+6+5+5=226+6+5+5=22.

In the second example it's optimal to choose candidates 11, 22 and 55. One person in each subject and the total sum is 6+6+11=236+6+11=23.

In the third example it's impossible to obtain a non-negative sum.

 

 

题意就是选科目,每科人数必须相同,总和尽量大。

有一个坑,可以往里面加入负数,只要该科总和>0就可以,具体代码。

 

代码:

 1 //C
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e5+10;
 6 const int inf=0x3f3f3f3f;
 7 
 8 vector<int> ve[maxn];
 9 vector<ll> sum[maxn];
10 int sz[maxn];
11 ll ans[maxn];
12 
13 bool cmp(int a,int b)
14 {
15     return a>b;
16 }
17 
18 int main()
19 {
20     int n,m;
21     cin>>n>>m;
22     for(int i=1;i<=n;i++){
23         int s,r;
24         cin>>s>>r;
25         ve[s].push_back(r);
26     }
27     for(int i=1;i<=m;i++)
28         sort(ve[i].begin(),ve[i].end(),cmp);
29     for(int i=1;i<=m;i++){
30         int pre=0;
31         for(int j=0;j<ve[i].size();j++){
32             if(j==0) sum[i].push_back(ve[i][j]),pre=ve[i][j];
33             else sum[i].push_back(pre+ve[i][j]),pre=sum[i][j];
34         }
35     }
36     int maxx=0;
37     for(int i=1;i<=m;i++){
38         maxx=max(maxx,(int)ve[i].size());
39         for(int j=0;j<ve[i].size();j++){
40             ans[j]=max(ans[j],ans[j]+sum[i][j]);
41         }
42     }
43     ll ret=0;
44     for(int i=0;i<maxx;i++)
45         ret=max(ret,ans[i]);
46     cout<<ret<<endl;
47 }

 

 

 

posted @ 2018-11-30 21:38  ZERO-  阅读(320)  评论(0编辑  收藏  举报