题目链接:https://codeforces.com/contest/1082/problem/C(C. Multi-Subject Competition)

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 (≤n≤≤n≤, ≤m≤≤m≤) — the number of candidates and the number of subjects. The next nn lines contains two integers per line: sisi and riri (≤si≤m1≤si≤m, −≤ri≤−≤ri≤) — 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 if every valid non-empty delegation has negative sum. Examples
input output input output input -
-
-
-
-
output 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.

题目  

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.

题目意思:

  给定n(备选人数),m(科目数目)。

  后n行输入,每行两个整数,x和y

  第i行输入,第i个学生     第x科目的y等级

  输出等级之和最好情况(唯一要求是      参赛的科目中人数是相等的)

 盲点:

  对于vector 类型来说,存在下列参数  greater<int>()  和less<int>()

    1. sort(v[i].begin(),v[i].end(),greater<int>());  //默认从大到小
    2. sort(v[i].begin(),v[i].end(),less<int>());    //默认从小到大
    3. 当然也可以手写cmp,传入cmp。

AC代码:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>a[];
int cmp(const int a,const int b){
return a>b;
}
int sum[];
int main()
{
int n,m;
while (cin>>n>>m){
vector <int>::iterator it;
int x,y;
memset(sum,,sizeof(sum));
for (int i=;i<n;i++){
cin>>x>>y;
a[x].push_back(y);
}
for (int i=;i<=n;i++){
sort(a[i].begin(),a[i].end(),cmp);
}
for (int i=;i<=m;i++){
for (int j=;j<a[i].size();j++){
a[i][j]+=a[i][j-];
}
}
int ans=;
for (int i=;i<=m;i++){
for (int j=;j<a[i].size();j++){
if (a[i][j]>){
sum[j]+=a[i][j];
// printf("sum[%d]=%d\n",j,sum[j]);
ans=max(ans,sum[j]);
}
}
a[i].clear();
}
printf("%d\n",ans);
}
return ;
}

vector的二维用法+前缀和的更多相关文章

  1. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  2. python排序之二冒泡排序法

    python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...

  3. 二值法方法综述及matlab程序

    在某些图像处理当中一个关键步是二值法,二值化一方面能够去除冗余信息,另一方面也会使有效信息丢失.所以有效的二值化算法是后续的处理的基础.比如对于想要最大限度的保留下面图的中文字,以便后续的定位处理. ...

  4. 智课雅思词汇---二十、前缀syn-sym-syl是什么意思

    智课雅思词汇---二十.前缀syn-sym-syl是什么意思 一.总结 一句话总结:l,m,n是可以互换 前缀:sy-, syn-, sym-, syl- [词根含义]:共同,同时 [词根来源]:(s ...

  5. Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)

    题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...

  6. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  7. C++ vector 实现二维数组时, 在类的头文件中定义时遇到"应输入类型符"的问题?

    见下,当我在类的声明文件中定义二维vector时,提示我应输入类型说明符; 但是相同的格式定义,在类中将二维vector修改为在源文件中定义就可以顺利通过,并顺利执行打印 打印结果如下: 望大神来解惑 ...

  8. vector创建二位数组

    默认初始化vector vector<vevtor<int> > arr(row, vector<int>(col, 0)); //指定行大小为row,列为col, ...

  9. vector作为二维数组

    vector本来就是可以用来代替一维数组的,vector提供了operator[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小. 这里只介绍用它来代替二维的数组,二维以上的可以依此类推. ...

随机推荐

  1. dedecms为导航栏目添加英文标题

    最近公司官网是使用 DedeCMS 做的,这个项目中要使用到为导航栏目添加英文标题,就查找资料把它实现了. 根据设计图写成静态页面后是这样的效果: 操作步骤如下: 1. 修改数据表,添加英文字段 影响 ...

  2. java开发优化常用的快捷键

    快速打印的输出语句:输入syso,然后按alt+/,即可 快速删除一行:ctrl+D 让代码块缩进:选中要缩进的代码,然后按tab键 让代码块退格:选中要退格的代码,然后按shift+tab键 格式化 ...

  3. BZOJ3174:[TJOI2013]拯救小矮人(DP)

    Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人, ...

  4. 「LGP4719【模板】动态dp」

    题目 尽管知道这个东西应该不会考了,但是还是学一学吧 哎要是去年noip之前学该多好 动态\(dp\)就是允许修改的一个\(dp\),比如这道题,我们都知道这是一个树上最大点权独立集 众所周知方程长这 ...

  5. [CQOI2006]凸多边形(半平面相交)

    嘟嘟嘟 本来我要写feng shui这道题的.然后网上都说什么半平面相交,于是我还得现学这个东西,就来刷这道模板题了. 所谓的半平面相交和高中数学的分数规划特别像.比如这道题,把每一条边看成一条有向直 ...

  6. Yii2.0 发送邮件时中文附件乱码的问题

    yii自带的邮件类使用的是MIME 协议,发送附件时用的是MIME 协议的 Content-disposition扩展,用扩展下载中文名称的附件时有时会正常,有时会乱码. 只需找到如下文件 的如下方法 ...

  7. LVS (Linux Virtual Server) 思维导图笔记

  8. C#ref和out的区别-ref是有进有出,out是只出不进

    之前学习C#时候就遇到了这个问题,不过当时没有深究.昨晚想到这个问题时候自己尝试敲了敲代码,结果从运行的结果来看,越看越乱.在查看了一些资料的基础上,自己总结了一下. 可能会有点乱,但是自己总结出来的 ...

  9. Java并发基础概念

    Java并发基础概念 线程和进程 线程和进程都能实现并发,在java编程领域,线程是实现并发的主要方式 每个进程都有独立的运行环境,内存空间.进程的通信需要通过,pipline或者socket 线程共 ...

  10. Redis入门(一)---安装

    一.Ubuntu安装 1.命令行安装 sudo apt-get install redis-server 2.启动redis服务(安装完成后自动启动) sudo /etc/init.d/redis-s ...