Submission #676449

# Submission time Handle Problem Language Result Execution time Memory
676449 2022-12-31T00:40:22 Z vjudge1 Schools (IZhO13_school) C++17
100 / 100
76 ms 7788 KB
#include <bits/stdc++.h>
using namespace std;

template <typename T> inline void read(T &n) {
  char c;

  do {
    c = getchar();
  }
  while (isdigit(c) == false && c != '-');

  bool negative = (c == '-');

  if (c == '-') {
    c = getchar();
  }

  n = 0;

  do {
    n = n * 10 + c - '0';
    c = getchar();
  }
  while (isdigit(c));

  if (negative) {
    n = -n;
  }
}

const int MAX_N = 300000;

pair <int, int> v[MAX_N + 1];

int64_t prefix[MAX_N + 1],
        suffix[MAX_N + 2];

int main() {
  int n, m, s;
  read(n); read(m); read(s);

  for (int i = 1; i <= n; i++) {
    auto &[a, b] = v[i];
    read(a); read(b);
  }

  sort(v + 1, v + n + 1, [] (auto p1, auto p2) {
    auto [a1, b1] = p1;
    auto [a2, b2] = p2;
    return (a1 - b1 > a2 - b2);
  });

  priority_queue <int, vector <int>, greater <int>> q;

  prefix[0] = 0;

  for (int i = 1; i <= n; i++) {
    auto [a, b] = v[i];

    int64_t &res = prefix[i];

    res = prefix[i - 1] + a;
    q.push(a);

    if (q.size() > m) {
      res -= q.top();
      q.pop();
    }
  }

  while (q.empty() == false) {
    q.pop();
  }

  suffix[n + 1] = 0;

  for (int i = n; i >= 1; i--) {
    auto [a, b] = v[i];

    int64_t &res = suffix[i];

    res = suffix[i + 1] + b;
    q.push(b);

    if (q.size() > s) {
      res -= q.top();
      q.pop();
    }
  }

  int64_t res = 0;

  for (int i = m; i <= n - s; i++) {
    res = max(res, prefix[i] + suffix[i + 1]);
  }

  cout << res;
}

Compilation message

school.cpp: In function 'int main()':
school.cpp:65:18: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   65 |     if (q.size() > m) {
      |         ~~~~~~~~~^~~
school.cpp:85:18: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   85 |     if (q.size() > s) {
      |         ~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 10 ms 1328 KB Output is correct
14 Correct 18 ms 2276 KB Output is correct
15 Correct 30 ms 3976 KB Output is correct
16 Correct 40 ms 5216 KB Output is correct
17 Correct 55 ms 6100 KB Output is correct
18 Correct 62 ms 6336 KB Output is correct
19 Correct 74 ms 6796 KB Output is correct
20 Correct 76 ms 7788 KB Output is correct