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>
#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
using namespace std;
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 2e5 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;
int n, k, ans = -INF, b[N];
pii a[N];
vi x;
struct node {
node *l, *r;
int cnt, sum;
node () {
l = r = NULL;
sum = cnt = 0;
}
node (int c, int s, int x) {
l = r = NULL;
cnt = c;
sum = s;
}
node (node *L, node *R) {
l = L;
r = R;
cnt = (!l ? 0 : l->cnt) + (!r ? 0 : r->cnt);
sum = (!l ? 0 : l->sum) + (!r ? 0 : r->sum);
}
};
typedef node * pnode;
pnode root[N];
pnode build (int tl = 0, int tr = N) {
if (tl == tr)
return new node();
int tm = (tl + tr) >> 1;
return new node(build(tl, tm), build(tm + 1, tr));
}
pnode update (pnode t, int pos, int tl = 0, int tr = N) {
if (tl == tr)
return new node(t->cnt + 1, t->sum + x[sz(x) - 1 - pos], 0);
int tm = (tl + tr) >> 1;
if (pos <= tm)
return new node(update(t->l, pos, tl, tm), t->r);
return new node(t->l, update(t->r, pos, tm + 1, tr));
}
int get (pnode t1, pnode t2, int k, int tl = 0, int tr = N) {
if (!k)
return 0;
if (tl == tr)
return x[sz(x) - tl - 1] * k;
int tm = (tl + tr) >> 1;
if (t1->l->cnt - t2->l->cnt >= k)
return get(t1->l, t2->l, k, tl, tm);
return t1->l->sum - t2->l->sum + get(t1->r, t2->r, k - t1->l->cnt + t2->l->cnt, tm + 1, tr);
}
int cost (int l, int r) {
return a[l].f + a[l].s + a[r].s - a[r].f + get(root[r - 1], root[l], k - 2);
}
void calc (int l = 1, int r = n - k + 1, int tl = 1, int tr = n) {
if (l > r)
return;
int m = (l + r) >> 1, mx = -INF, tm = -1;
for (int i = max(m + k - 1, tl); i <= tr; ++i) {
int cur = cost(m, i);
if (cur > mx) {
mx = cur;
tm = i;
}
}
ans = max(ans, mx);
calc(l, m - 1, tl, tm);
calc(m + 1, r, tm, tr);
}
main () {
scanf("%lld%lld", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &a[i].s, &a[i].f);
a[i].f += a[i].f;
x.pb(a[i].s);
}
sort(a + 1, a + 1 + n);
sort(all(x));
x.resize(unique(all(x)) - x.begin());
root[0] = build();
for (int i = 1; i <= n; ++i) {
b[i] = lower_bound(all(x), a[i].s) - x.begin();
root[i] = update(root[i - 1], sz(x) - 1 - b[i]);
}
calc();
cout << ans << endl;
}
Compilation message (stderr)
cake3.cpp:122:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
cake3.cpp: In function 'int main()':
cake3.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~
cake3.cpp:125:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld", &a[i].s, &a[i].f);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |