Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define FOR(i, a, b) for (int i = (a); i < (b); i++)
- #define REP(i, n) FOR(i, 0, n)
- #define ll long long
- using namespace std;
- const int N = 500100;
- int n;
- int T[N];
- ll V[N];
- namespace brut {
- const int MAXN = 4010;
- ll dp[MAXN][2];
- multiset <ll> ms;
- ll brut1() {
- ll out = 0;
- REP(i, MAXN) dp[i][0] = dp[i][1] = -(1LL << 60);
- dp[0][0] = 0;
- REP(i, n) {
- int t = T[i];
- ll v = V[i];
- REP(j, n) {
- dp[j][1] = dp[j][0];
- if (t == 2 && j > 0) dp[j][1] = max(dp[j][1], dp[j - 1][0] - v);
- if (t == 1) dp[j][1] = max(dp[j][1], dp[j + 1][0] + v);
- out = max(out, dp[j][1]);
- }
- REP(j, n) dp[j][0] = dp[j][1];
- }
- return out;
- }
- /*ll brut2() {
- ll out = 0;
- REP(i, n) {
- int t = T[i];
- ll v = V[i];
- if (t == 1) {
- if (ms.empty()) continue;
- ll curr = *ms.begin();
- if (curr < v) {
- out += v - curr;
- ms.erase(ms.begin());
- }
- } else {
- ms.insert(v);
- }
- }
- return out;
- }*/
- /*ll init() {
- if (n <= 2000) return brut1();
- else return brut2();
- return -1;
- }*/
- }
- namespace solution {
- multiset <ll> a, b;
- ll init() {
- a.clear();
- b.clear();
- ll out = 0;
- REP(i, n) {
- int t = T[i];
- ll v = V[i];
- if (t == 1) {
- if (a.size() == 0 || (*a.begin()) >= v) {
- if (b.size() == 0 || (*b.begin()) >= v) continue;
- out += v - (*b.begin());
- b.erase(b.begin());
- b.insert(v);
- } else {
- if (b.size() == 0 || (*a.begin()) <= (*b.begin())) {
- out += v - (*a.begin());
- a.erase(a.begin());
- b.insert(v);
- } else {
- out += v - (*b.begin());
- b.erase(b.begin());
- b.insert(v);
- }
- }
- } else {
- a.insert(v);
- }
- }
- return out;
- }
- }
- /*namespace generator {
- void init() {
- n = rand() % 300 + 1700;
- int l1 = rand() * rand() % 10000 + 1, l2 = rand() * rand() % 10000 + 1;
- REP(i, n) {
- T[i] = (rand() & 1) + 1;
- if (T[i] == 1) V[i] = rand() * rand() % l1;
- else V[i] = rand() * rand() % l2;
- }
- return;
- }
- }*/
- /*namespace testing {
- void init() {
- srand(time(0));
- int cnt = 0;
- ll A = 0, B = 0;
- while (A == B) {
- generator::init();
- cout << n << " " << cnt << endl;
- A = brut::init(), B = solution::init();
- if (A != B) {
- cout << n << "\n";
- REP(i, n) {
- cout << T[i] << " " << V[i] << "\n";
- }
- cout << A << " " << B << "\n";
- }
- cout << A << " " << B << endl << endl;
- cnt++;
- assert(A == B);
- }
- return;
- }
- }*/
- int main() {
- ios_base::sync_with_stdio(false);
- //testing::init();
- cin >> n;
- REP(i, n) cin >> T[i] >> V[i];
- if (n <= 2000) cout << brut::brut1() << "\n";
- else cout << solution::init() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement