이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
using db = long double;
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
vi A(N), B(N); for(int i = 0; i < N; i++) cin >> A[i] >> B[i];
vi I(N); iota(begin(I), end(I), 0); sort(begin(I), end(I), [&](int x, int y) {
return B[x] < B[y];
});
auto check = [&](int x) {
// cout << x << endl;
ll ans = -INFL;
vi in; ll S = 0;
for(auto& i : I) if (A[i] >= x) {
S += A[i]; in.pb(i);
if (sz(in) >= M) {
// cout << sz(in) << " " << M << endl;
if (sz(in) > M) S -= A[in[sz(in) - M - 1]];
int lst = in[sz(in) - M], fir = in[sz(in) - 1];
// cout << fir << " " << lst << " => " << S << endl;
ans = max(ans, S - 2LL * (B[fir] - B[lst]));
// cout << x << " " << i << " => " << S - 2 * (B[fir] - B[lst]) << endl;
}
}
// cout << ans << endl;
return ans;
};
vi X = A; sort(begin(X), end(X));
int lo = 0, hi = sz(X) - M - 1;
while(lo < hi) {
int mid = (lo + hi) / 2;
// cout << mid << endl;
if (check(X[mid]) > check(X[mid+1])) hi = mid;
else lo = mid + 1;
// cout << endl << endl;
}
// cout << lo << endl;
cout << check(X[lo]) << nl;
exit(0-0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |