Skip to content

Instantly share code, notes, and snippets.

@ZhongxuanWang
Created December 31, 2020 18:45
Show Gist options
  • Save ZhongxuanWang/ca991d47879b2159358448e5478effc5 to your computer and use it in GitHub Desktop.
Save ZhongxuanWang/ca991d47879b2159358448e5478effc5 to your computer and use it in GitHub Desktop.
Codeforces Problem - Lala Land and Apple Trees (https://codeforces.com/contest/558/problem/A)
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<pair<int, int> > ps;
vector<pair<int, int> > ns;
int nnegs = 0;
int nposs = 0;
for (int i = 0; i < N; i ++) {
int first, second;
cin >> first >> second;
if (first > 0) {
ps.push_back( make_pair(first, second));
nposs++;
} else {
ns.push_back( make_pair(first, second));
nnegs++;
}
}
sort(ps.begin(), ps.end(), [] (const pair<int, int> &p1, const pair<int, int> &p2) {return p1.first < p2.first;});
sort(ns.begin(), ns.end(), [] (const pair<int, int> &p1, const pair<int, int> &p2) {return p1.first > p2.first;});
int ans = 0;
int i;
for (i =0; i < min(nposs, nnegs); i ++) {
ans += ps[i].second;
ans += ns[i].second;
}
if (nnegs!=nposs) {
if (nposs > nnegs) {
ans+=ps[i].second;
} else {
ans+=ns[i].second;
}
}
cout << ans << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment