This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define MOEZ ios_base::sync_with_stdio(false);cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define F first
#define S second
#define pb push_back
#define all(v) v.begin() , v.end()
#define eb emplace_back
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define mem(d , x) memset(d , x , sizeof(d));
#define sz(x) (int)x.size()
//const int N = 2e5+5;
//const int M = 1e9+7;
//const ll inf = 1e17;
//const ld eps = 1e-6;
#define int long long
int n , s = 0;
multiset<pair<int , int>>m;
signed main(){
MOEZ
cin >> n;
for(int i = 0; i < n;i++){
int a , b;
cin >> a >> b;
m.insert({-a, b});
s += b;
}
int ans = -1e17;
for(int i = 0; i < n;i++){
auto it = m.begin();
auto it2 = m.end();
it2--;
if(sz(m) == 1){
ans = max(ans , s);
break;
}
int mx = it->F, mn = it2->F;
mx *= -1 , mn *= -1;
ans = max(ans , s-(mx-mn));
if(it->S <= it2->S){
s -= it->S;
m.erase(it);
}
else{
s -= it2->S;
m.erase(it2);
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |