이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
void DBG() { cerr << "]" << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#define debug(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
const int INF = 1e9 + 7, N = 2e5 + 7;
const ll oo = 1e18 + 7;
// qry(l, r, m): sum of max m things in [l, r)
int b[N];
ll qry(int l, int r, int m) {
assert(r - l >= m);
ll ans = 0;
vi tt;
for(int i = l; i < r; i++)
tt.PB(b[i]);
sort(ALL(tt), greater<int>());
for(int i = 0; i < m; i++)
ans += tt[i];
return ans;
}
signed main() {
IO_OP;
int n, m;
cin >> n >> m;
m -= 2;
V<pi> a(n); // c, v
for(int i = 0; i < n; i++) {
cin >> a[i].S >> a[i].F;
}
sort(ALL(a));
for(int i = 0; i < n; i++)
b[i] = a[i].S;
auto cost = [&] (int j, int i) {
if(i - j - 1 < m) return -oo;
return 0LL + 2 * a[j].F - 2 * a[i].F + a[i].S + a[j].S + qry(j + 1, i, m);
};
ll ans = -oo;
function<void(int, int, int, int)> solve = [&] (int l, int r, int ql, int qr) {
if(l > r)
return;
if(r == l) {
for(int i = ql; i <= qr; i++)
ans = max(ans, cost(i, l));
return;
}
int m = (l + r) / 2;
ll mx = -oo, qm = -1;
for(int i = ql; i <= qr; i++) {
ll tt = cost(i, m);
if(tt > mx) {
mx = tt;
qm = i;
}
}
assert(qm != -1);
ans = max(ans, mx);
solve(l, m - 1, ql, qm);
solve(m + 1, r, qm, qr);
};
solve(m + 1, n - 1, 0, n - 1);
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |