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>> pending;     int to_double = 0;     int l_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) {         pending.insert({d, t});       } else {         auto search = doubled.find({-d, t});         if (search != doubled.end()) {           doubled.erase(search);           l_doubled -= t;           total_damage += d;         } else {           pending.erase(pending.find({-d, t}));         }       }       if (to_double > doubled.size()) {         if (pending.size() > 0) {           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           doubled.insert(max);           pending.erase(it_pending);           l_doubled += max.second;           total_damage += max.first;         }       } else if (to_double < doubled.size()) {         if (doubled.size() > 0) {           auto it_doubled = doubled.begin();           std::pair<int, int> min = *it_doubled;           doubled.erase(it_doubled);           pending.insert(min);           l_doubled -= min.second;           total_damage -= min.first;         }       } else {         if (to_double > 0 && pending.size() > 0) {           auto it_doubled = doubled.begin();           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           std::pair<int, int> min = *it_doubled;           if (max > min) {             doubled.erase(it_doubled);             pending.erase(it_pending);             doubled.insert(max);             pending.insert(min);             l_doubled += max.second - min.second;             total_damage += max.first - min.first;           }         }       }       if (doubled.size() == to_double && l_doubled == to_double &&           to_double > 0) {         auto it_doubled = doubled.begin();         std::pair<int, int> min = *it_doubled;         doubled.erase(it_doubled);         total_damage -= min.first;         l_doubled--;         if (pending.size() > 0) {           auto it_pending = --pending.end();           std::pair<int, int> max = *it_pending;           pending.erase(it_pending);           doubled.insert(max);           total_damage += max.first;         }         pending.insert(min);       }       std::cout << total_damage << std::endl;     }     return 0;   }   