1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #include <bits/stdc++.h> #define ld double #define ull unsigned long long #define ll long long #define pii pair <int, int> #define iiii pair <int, pii > #define mp make_pair #define INF 1000000000 #define rep(i, x) for(int (i) = 0; (i) < (x); (i)++) inline int getint() { int x = 0, p = 1; char c = getchar(); while (c <= 32) c = getchar(); if (c == 45) p = -p, c = getchar(); while (c > 32) x = x * 10 + c - 48, c = getchar(); return x * p; } using namespace std; const int mod = 1e9 + 7; inline void reduce(int &x) { x += x >> 31 & mod; } inline int mul(int x, int y) { return 1ll * x * y % mod; } //ruogu_alter const int N = 1e6 + 5; int n, k, sz[N], rd[N], ls[N], rs[N], rt, a[N]; ll b[N], res; ll val[N], tag_val[N]; int rk[N], tag_rk[N]; vector<pair<int, ll>> aa; // inline ll calc(int x, int y) { return 1ll * x * a[y] + b[y]; } inline void upd_rk(int k, int x) { rk[k] += x; tag_rk[k] += x; } inline void upd_val(int k, ll x) { val[k] += x; tag_val[k] += x; } inline void up(int k) { sz[k] = sz[ls[k]] + sz[rs[k]] + 1; } inline void pd(int k) { if (tag_rk[k]) { upd_rk(ls[k], tag_rk[k]); upd_rk(rs[k], tag_rk[k]); tag_rk[k] = 0; } if (tag_val[k]) { upd_val(ls[k], tag_val[k]); upd_val(rs[k], tag_val[k]); tag_val[k] = 0; } } void merge(int x, int y, int &z) { if (!x || !y) { z = x ^ y; return; } pd(x); pd(y); //if (rnd[x] < rnd[y]) { //if (rand() % (sz[x] + sz[y]) < sz[x]) { if (rand() & 1) { merge(rs[x], y, rs[x]); z = x; } else { merge(x, ls[y], ls[y]); z = y; } up(z); } void split(int x, int &y, int &z, int id) { if (!x) { y = z = 0; return; } pd(x); if (val[x] < calc(rk[x], id)) { split(rs[x], rs[x], z, id); y = x; } else { split(ls[x], y, ls[x], id); z = x; } up(x); } void dfs(int x) { if (!x) return; if (rk[x] < k) res += val[x]; pd(x); dfs(ls[x]); dfs(rs[x]); } int main() { srand(233); n = getint(); k = getint(); rep(i, n) { int x = getint(); ll y; scanf("%lld", &y); aa.emplace_back(x, y); } sort(aa.begin(), aa.end()); reverse(aa.begin(), aa.end()); rep(i, aa.size()) { a[i] = aa[i].first; b[i] = aa[i].second; } rt = 1; val[1] = calc(0, 0); sz[1] = 1; for (int i = 1; i < n; i++) { int x, y; split(rt, x, y, i); if (y) { upd_rk(y, 1); upd_val(y, a[i]); } rt = i + 1; sz[rt] = 1; rk[rt] = x ? sz[x] : 0; val[rt] = calc(rk[rt], i); merge(x, rt, rt); merge(rt, y, rt); } dfs(rt); cout << res << endl; return 0; }
|