#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
int n, mpos[1000001]; vector<pair<int, int> > msts;
long long ts = 0;
bool check(int ms){
long long carry = 0;
for (int i = 100000; i > 0; i--){
if (mpos[i] > ms){
carry += mpos[i]-ms;
} else {
carry = max((carry-ms+mpos[i]), 0ll);
}
}
if (carry > 0){
return false;
}
return true;
}
long long ga(int ms){
long long ans = 0, sum = 0, carry = 0;
for (int i = 5; i > 0; i--){
mpos[i] += carry; long long su = min(ms-1, mpos[i]);
carry = mpos[i] - su; ans += ((su-1)*su)/2; sum += su;
}
ans += (ts-sum) * (ms-1);
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n;
for (int i = 0; i < n; i++){
int a, b; cin >> a >> b;
msts.push_back(make_pair(a, b)); ts += b;
}
sort(msts.begin(), msts.end());
for (int i = 0; i < n/2; i++){
swap(msts[i], msts[n-i-1]);
}
int mstp = 0; priority_queue<int> pq;
for (int i = 100000; i > 0; i--){
while (mstp < n && i == msts[mstp].first){
pq.push(i-msts[mstp].second); mstp++;
}
while (pq.size() > 0 && pq.top() == i){
pq.pop();
}
mpos[i] = pq.size();
}
int low = 1, high = n;
while (true){
if (low == high){
cout << ga(low) << endl; return 0;
} else if (low + 1 == high){
if (check(low)){
cout << ga(low) << endl; return 0;
}
cout << ga(high) << endl; return 0;
}
int mid = (low+high)/2;
if (check(mid)){
high = mid;
} else {
low = mid;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
748 KB |
Output is correct |
2 |
Incorrect |
2 ms |
748 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
1004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
1648 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
1900 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
3048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
46 ms |
3428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
3044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |