Submission #16937

# Submission time Handle Problem Language Result Execution time Memory
16937 2015-10-27T18:10:15 Z erdemkiraz Schools (IZhO13_school) C++
100 / 100
365 ms 18060 KB
#include <bits/stdc++.h>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + inf;

const int N = 3e5 + 5;

int n, m, s;
ii a[N];

int main() {

    scanf("%d %d %d", &n, &m, &s);

    for(int i = 1; i <= n; i++) {
        scanf("%d %d", &a[i].second, &a[i].first);
    }

    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);

    for(int i = 1; i <= n; i++)
        swap(a[i].first, a[i].second);

    ll sum = 0, add = 0, ans = 0;

    set < ii > s1, s2;

    for(int i = 1; i < s; i++) {
        sum += a[i].second;
        s1.insert(ii(a[i].first - a[i].second, i));
    }

    for(int i = s; i <= n; i++) {
        s2.insert(ii(a[i].first, i));
    }

    while(s2.size() > m + 1) {
        s2.erase(*s2.begin());
    }

    foreach(it, s2) {
        add += it -> first;
    }

    if(!s) {
        printf("%lld\n", add - s2.begin() -> first);
        return 0;
    }

    if(!m) {
        printf("%lld\n", sum);
        return 0;
    }

    for(int i = s; ; i++) {
        if(!s2.size())
            break;
        if(s2.find(ii(a[i].first, i)) == s2.end()) {
            add -= s2.begin() -> first;
            s2.erase(s2.begin());
        }
        else {
            add -= a[i].first;
            s2.erase(ii(a[i].first, i));
        }
        ll res = sum + add + a[i].second;
        ans = max(ans, res);
        ii best = s1.size() ? *s1.rbegin() : ii(0, 0);
        if(!s1.size() or a[i].first > a[i].second + best.first) {
            sum += a[i].first;
        }
        else {
            sum += a[i].second + best.first;
            s1.erase(*s1.rbegin());
            s1.insert(ii(a[i].first - a[i].second, i));
        }
    }

    printf("%lld\n", ans);

    return 0;

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 4068 KB Output is correct
2 Correct 0 ms 4068 KB Output is correct
3 Correct 0 ms 4068 KB Output is correct
4 Correct 0 ms 4068 KB Output is correct
5 Correct 0 ms 4068 KB Output is correct
6 Correct 0 ms 4068 KB Output is correct
7 Correct 0 ms 4200 KB Output is correct
8 Correct 0 ms 4200 KB Output is correct
9 Correct 3 ms 4200 KB Output is correct
10 Correct 0 ms 4200 KB Output is correct
11 Correct 4 ms 4200 KB Output is correct
12 Correct 0 ms 4200 KB Output is correct
13 Correct 42 ms 5652 KB Output is correct
14 Correct 63 ms 7632 KB Output is correct
15 Correct 151 ms 11460 KB Output is correct
16 Correct 179 ms 12384 KB Output is correct
17 Correct 236 ms 14232 KB Output is correct
18 Correct 272 ms 15288 KB Output is correct
19 Correct 303 ms 16212 KB Output is correct
20 Correct 365 ms 18060 KB Output is correct