func_code_string: #include <bits/stdc++.h>   int main() {     int N;     std::cin >> N;     std::set<std::pair<int, int>> doubled;     std::set<std::pair<int, int>> pend;     int to_double = 0;     int f_doubled = 0;     int64_t total_damage = 0;     int t;     int d;     for (int i = 0; i < N; i++) {       std::cin >> t;       std::cin >> d;       total_damage += d;       to_double += t * (d > 0 ? 1 : -1);       if (d > 0) {         pend.insert({d, t});       } else {         auto search = doubled.find({-d, t});         if (search != doubled.end()) {           doubled.erase(search);           if (t == 0) f_doubled--;           total_damage += d;         } else {           pend.erase(pend.find({-d, t}));         }         if (t == 1) {           if (doubled.size() > 0) {             auto it_doubled = doubled.begin();             std::pair<int, int> max = *it_doubled;             doubled.erase(it_doubled);             pend.insert(max);             if (max.second == 0) f_doubled--;             total_damage -= max.first;           }         }       }       if (to_double > doubled.size()) {         if (pend.size() > 0) {           auto it_pend = --pend.end();           std::pair<int, int> max = *it_pend;           doubled.insert(max);           pend.erase(it_pend);           if (max.second == 0) f_doubled++;           total_damage += max.first;         }       } else if (doubled.size() == to_double) {         if (to_double > 0 && pend.size() > 0) {           auto it_doubled = doubled.begin();           auto it_pend = --pend.end();           std::pair<int, int> min = *it_doubled;           std::pair<int, int> max = *it_pend;           if (max.first > min.first) {             if (max.second == 0) f_doubled++;             if (min.second == 0) f_doubled--;             doubled.erase(it_doubled);             pend.erase(it_pend);             doubled.insert(max);             pend.insert(min);             total_damage += max.first - min.first;           }         }       }       if (doubled.size() > 0 && f_doubled == 0 && doubled.size() == to_double) {         auto it_doubled = doubled.begin();         doubled.erase(it_doubled);         std::pair<int, int> min = *it_doubled;         total_damage -= min.first;         if (pend.size() > 0) {           auto it_pend = --pend.end();           std::pair<int, int> max = *it_pend;           pend.erase(it_pend);           doubled.insert(max);           f_doubled++;           total_damage += max.first;         }         pend.insert(min);       }       printf( %lld n , total_damage);     }     return 0;   }   