Submission #873763

# Submission time Handle Problem Language Result Execution time Memory
873763 2023-11-15T16:57:59 Z MilosMilutinovic Schools (IZhO13_school) C++14
100 / 100
213 ms 25560 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, s;
  cin >> n >> m >> s;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  vector<int> order(n);
  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    return a[i] > a[j] || (a[i] == a[j] && b[i] < b[j]);
  });
  vector<int> new_a(n), new_b(n);
  for (int i = 0; i < n; i++) {
    new_a[i] = a[order[i]];
    new_b[i] = b[order[i]];
  }
  a = new_a;
  b = new_b;
  order.clear();
  for (int i = m; i < n; i++) {
    order.push_back(i);
  }
  sort(order.begin(), order.end(), [&](int i, int j) {
    return b[i] > b[j];
  });
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    ans += a[i];
  }
  for (int i = 0; i < s; i++) {
    ans += b[order[i]];
  }
  multiset<int> L;
  for (int i = 0; i < m; i++) {
    L.insert(-a[i] + b[i]);
  }
  multiset<pair<int, int>> R;
  multiset<int> Ra;
  multiset<pair<int, int>> Rab;
  for (int i = 0; i < s; i++) {
    R.emplace(-b[order[i]], order[i]);
    Rab.emplace(-b[order[i]] + a[order[i]], order[i]);
  }
  for (int i = s; i < (int) order.size(); i++) {
    Ra.insert(a[order[i]]);
  }
  long long res = ans;
  while (!L.empty() && !R.empty()) {
    assert(!Rab.empty());
    res += *prev(L.end());
    L.erase(prev(L.end()));
    if (Ra.empty() || prev(Rab.end())->first > prev(R.end())->first + *prev(Ra.end())) {
      auto it = prev(Rab.end());
      res += it->first;
      int i = it->second;
      R.erase(R.find({-b[i], i}));
      Rab.erase(it);
    } else {
      {
        auto it = prev(R.end());
        res += it->first;
        int i = it->second;
        R.erase(R.find({-b[i], i}));
        Rab.erase(Rab.find({-b[i] + a[i], i}));
        Ra.insert(a[i]);
      }
      {
        auto it = prev(Ra.end());
        res += *it;
        Ra.erase(it);
      }
    }
    ans = max(ans, res);
  }
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 3 ms 952 KB Output is correct
12 Correct 2 ms 860 KB Output is correct
13 Correct 13 ms 2888 KB Output is correct
14 Correct 54 ms 6484 KB Output is correct
15 Correct 85 ms 11088 KB Output is correct
16 Correct 137 ms 19744 KB Output is correct
17 Correct 167 ms 20164 KB Output is correct
18 Correct 162 ms 20048 KB Output is correct
19 Correct 189 ms 22464 KB Output is correct
20 Correct 213 ms 25560 KB Output is correct