Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FOR(i, a, b) for (int i = (a); i < (b); i++)
  4. #define REP(i, n) FOR(i, 0, n)
  5. #define ll long long
  6.  
  7. using namespace std;
  8.  
  9. const int N = 500100;
  10. int n;
  11. int T[N];
  12. ll V[N];
  13.  
  14. namespace brut {
  15. const int MAXN = 4010;
  16. ll dp[MAXN][2];
  17. multiset <ll> ms;
  18.  
  19. ll brut1() {
  20. ll out = 0;
  21. REP(i, MAXN) dp[i][0] = dp[i][1] = -(1LL << 60);
  22. dp[0][0] = 0;
  23.  
  24. REP(i, n) {
  25. int t = T[i];
  26. ll v = V[i];
  27.  
  28. REP(j, n) {
  29. dp[j][1] = dp[j][0];
  30. if (t == 2 && j > 0) dp[j][1] = max(dp[j][1], dp[j - 1][0] - v);
  31. if (t == 1) dp[j][1] = max(dp[j][1], dp[j + 1][0] + v);
  32.  
  33. out = max(out, dp[j][1]);
  34. }
  35.  
  36. REP(j, n) dp[j][0] = dp[j][1];
  37. }
  38.  
  39. return out;
  40. }
  41.  
  42. /*ll brut2() {
  43. ll out = 0;
  44.  
  45. REP(i, n) {
  46. int t = T[i];
  47. ll v = V[i];
  48.  
  49. if (t == 1) {
  50. if (ms.empty()) continue;
  51.  
  52. ll curr = *ms.begin();
  53. if (curr < v) {
  54. out += v - curr;
  55. ms.erase(ms.begin());
  56. }
  57. } else {
  58. ms.insert(v);
  59. }
  60. }
  61.  
  62. return out;
  63. }*/
  64.  
  65. /*ll init() {
  66. if (n <= 2000) return brut1();
  67. else return brut2();
  68.  
  69. return -1;
  70. }*/
  71. }
  72.  
  73. namespace solution {
  74. multiset <ll> a, b;
  75.  
  76. ll init() {
  77. a.clear();
  78. b.clear();
  79. ll out = 0;
  80.  
  81. REP(i, n) {
  82. int t = T[i];
  83. ll v = V[i];
  84.  
  85. if (t == 1) {
  86. if (a.size() == 0 || (*a.begin()) >= v) {
  87. if (b.size() == 0 || (*b.begin()) >= v) continue;
  88.  
  89. out += v - (*b.begin());
  90. b.erase(b.begin());
  91. b.insert(v);
  92. } else {
  93. if (b.size() == 0 || (*a.begin()) <= (*b.begin())) {
  94. out += v - (*a.begin());
  95. a.erase(a.begin());
  96. b.insert(v);
  97. } else {
  98. out += v - (*b.begin());
  99. b.erase(b.begin());
  100. b.insert(v);
  101. }
  102. }
  103. } else {
  104. a.insert(v);
  105. }
  106. }
  107.  
  108. return out;
  109. }
  110. }
  111.  
  112. /*namespace generator {
  113. void init() {
  114. n = rand() % 300 + 1700;
  115. int l1 = rand() * rand() % 10000 + 1, l2 = rand() * rand() % 10000 + 1;
  116. REP(i, n) {
  117. T[i] = (rand() & 1) + 1;
  118. if (T[i] == 1) V[i] = rand() * rand() % l1;
  119. else V[i] = rand() * rand() % l2;
  120. }
  121. return;
  122. }
  123. }*/
  124.  
  125. /*namespace testing {
  126. void init() {
  127. srand(time(0));
  128.  
  129. int cnt = 0;
  130. ll A = 0, B = 0;
  131. while (A == B) {
  132. generator::init();
  133. cout << n << " " << cnt << endl;
  134. A = brut::init(), B = solution::init();
  135.  
  136. if (A != B) {
  137. cout << n << "\n";
  138. REP(i, n) {
  139. cout << T[i] << " " << V[i] << "\n";
  140. }
  141. cout << A << " " << B << "\n";
  142. }
  143.  
  144. cout << A << " " << B << endl << endl;
  145. cnt++;
  146.  
  147. assert(A == B);
  148. }
  149.  
  150. return;
  151. }
  152. }*/
  153.  
  154. int main() {
  155. ios_base::sync_with_stdio(false);
  156.  
  157. //testing::init();
  158.  
  159. cin >> n;
  160. REP(i, n) cin >> T[i] >> V[i];
  161. if (n <= 2000) cout << brut::brut1() << "\n";
  162. else cout << solution::init() << "\n";
  163.  
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement