题目链接: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. Android App 注射&&Drozer Use

    x01 准备工作 测试环境: 1) 手机root权限 2) Adb.exe 3) 手机usb连接开启debug模式(在设置>关于手机>连续点击多次版本号,即可开启开发者模式) 4) Win ...

  2. mac本机svn命令使用

    公司项目用到svn,之前做版本管理用的是git. 现在对svn回顾学习了一下. 这里有一篇很好的入门教程 http://www.rubyrobot.org/tutorial/subversion-wi ...

  3. F2eTest和uirecorder自动化测试环境部署填坑记录

    坑1:尝试部署的时候只在opennode.bat里面填写了两个浏览器,测试通过后再增加其他浏览器,页面上一直不显示. 填坑:需要清空数据库里的`wd_browsers`和`wd_nodes`表,然后重 ...

  4. Vue实现购物小球抛物线

    .shop{ position: fixed; top: 300px; left: 40px; } .ball{ position: fixed; left: 32px; bottom: 22px; ...

  5. 23、springboot与缓存(1)

    一.JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. 1.CachingPro ...

  6. 人人开源之代码生成器(renren-generator)

    本篇文章,主要包含三个部分,介绍.代码生成演示.代码分析(不会很深入)等 三个部分足以让你学会使用,实际生产可能遇到的问题不会在这遇到. 代码生成器的作用在于提高开发效率.但是这个代码生成器仍有其局限 ...

  7. mysql5.6编译安装

    1.安装编译源码所需的工具和库 yum install gcc gcc-c++ ncurses-devel perl -y yum -y install wget gcc-c++ ncurses nc ...

  8. python接口自动化读取json,yaml配置文件+封装requests+unittest+HTMLRunner实现全自动化

    # coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...

  9. ubuntu 14.04 将窗体button移到右边

    刚刚安装了Ubuntu 14.04,想改动窗体button的位置.但依照曾经的办法发现不行了,在gconftool-->apps中找不到metacity. 多方查找后找到解决方式,例如以下 Ub ...

  10. 微服务之数据同步Porter

    Porter是一款数据同步中间件,主要用于解决同构/异构数据库之间的表级别数据同步问题. 背景 在微服务架构模式下深刻的影响了应用和数据库之间的关系,不像传统多个服务共享一个数据库,微服务架构下每个服 ...