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

C. Multi-Subject Competition

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 (1≤n≤1051≤n≤105, 1≤m≤1051≤m≤105) — the number of candidates and the number of subjects.

The next nn lines contains two integers per line: sisi and riri (1≤si≤m1≤si≤m, −104≤ri≤104−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 }

原文地址:https://www.cnblogs.com/ZERO-/p/10046655.html

时间: 2024-10-02 05:18:12

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

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition 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 

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

C. Multi-Subject Competition time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A multi-subject competition is coming! The competition has m different subjects participants can choose from. That'

codeforces Educational Codeforces Round 55 (Rated for Div. 2) C题 C. Multi-Subject Competition

这道题比赛时候没做出来,下来一看才发现是排序傻逼题. 把每个偏好的人做成一个vector,从大到小排序,做一个前缀和.然后将每种人数做一个桶,在桶里装每种科目选择人数为i的时候分数总和. 遍历每一维vector,把各个位置上面的vector加到sum数组中,最后sum数组里面挑出最大值. #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> vec[100010];//vecto

[Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition][时间复杂度]

https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n,m<=1e5) 题解:用vector存下每种类型的各个v并且每种类型根据v按从大到小排序,然后处理出每种类型的前缀和,然后扫每种类型的所有前缀和,如果该类型在i处的前缀和大于0,则相应的ans[i]加上这个类型在i处的前缀和,最后求出max(ans[i])(1<=i<=n)即可. 注意:这题

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Graph constructive problems are back! This time the graph you are asked to build should match the following proper

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies

传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你最多可以交换其中两个奖杯,求最大的连续的金奖杯个数. 题解: 思路: 求出连续的金奖杯位置,找出每两个相邻的连续的金奖杯所能够形成的最大的连续的金奖杯的个数,输出最大值. 相关变量解释: 1 int n; 2 char trophy[maxn]; 3 struct Node 4 { 5 int l,

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ