usaco-camp

Short, concise solutions for problems from the USACO camp

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	int N, L;
	cin >> N >> L;

	multiset<ll> S;
	ll ans = 0;
	for (int i = 0, b, s; i < N; ++i) {
		cin >> b >> s;
		ans = max(ans - (i >= L ? *begin(S) : 0) + s, ans);
		S.insert({ b, s });
		if (i >= L) S.erase(begin(S)), S.erase(prev(end(S)));
	}
	for (auto it = S.begin(); L; ++it, --L) ans -= *it;
	cout << ans << endl;
}