#include <bits/stdc++.h>
using namespace std;
struct bucket {
  long long ans[5];
  set<long long> s;
} bu[350];
long long buckets = 1;
void add(long long x) {
  for (long long i = 0; i < buckets; ++i) {
    if (bu[i].s.size() == 0) {
      bu[i].s.insert(x);
      bu[i].ans[0] = x;
      return;
    }
    if (x < *bu[i].s.begin()) {
      bu[i].s.insert(x);
      long long temp = bu[i].ans[4];
      for (long long j = 4; j > 0; --j) bu[i].ans[j] = bu[i].ans[j - 1];
      bu[i].ans[0] = temp + x;
      if (bu[i].s.size() < 301) return;
      x = *(--bu[i].s.end());
      bu[i].s.erase(--bu[i].s.end());
      bu[i].ans[0] -= x;
      if (i == buckets - 1) ++buckets;
      continue;
    }
    if (*(--bu[i].s.end()) > x || bu[i].s.size() < 300) {
      bool cont = 1;
      bu[i].s.insert(x);
      if (bu[i].s.size() <= 300)
        cont = 0;
      else {
        x = *(--bu[i].s.end());
        bu[i].s.erase(--bu[i].s.end());
      }
      memset(bu[i].ans, 0, sizeof bu[i].ans);
      long long j = 0;
      for (set<long long>::iterator it = bu[i].s.begin(); it != bu[i].s.end();
           ++it, ++j) {
        if (j == 5) j = 0;
        bu[i].ans[j] += *it;
      }
      if (cont == 0) return;
      if (i == buckets - 1) ++buckets;
      continue;
    }
    if (i == buckets - 1) ++buckets;
  }
}
void del(long long x) {
  long long from = -1;
  for (long long i = buckets - 1; i >= 0; --i) {
    if (bu[i].s.size() == 0) {
      --buckets;
      continue;
    }
    if (bu[i].s.count(x)) {
      bu[i].s.erase(x);
      if (from != -1) bu[i].s.insert(from);
      memset(bu[i].ans, 0, sizeof bu[i].ans);
      if (bu[i].s.size() == 0) {
        --buckets;
        return;
      }
      long long j = 0;
      for (set<long long>::iterator it = bu[i].s.begin(); it != bu[i].s.end();
           ++it, ++j) {
        if (j == 5) j = 0;
        bu[i].ans[j] += *it;
      }
      return;
    }
    if (from != -1) {
      bu[i].ans[0] += from;
      bu[i].s.insert(from);
    }
    from = *bu[i].s.begin();
    bu[i].s.erase(bu[i].s.begin());
    bu[i].ans[0] -= from;
    long long temp = bu[i].ans[0];
    for (long long i = 0; i < 4; ++i) bu[i].ans[i] = bu[i].ans[i + 1];
    bu[i].ans[4] = temp;
    if (bu[i].s.size() == 0) --buckets;
  }
}
int main() {
  long long n;
  cin >> n;
  char a[5];
  for (long long i = 0; i < n; ++i) {
    scanf("%s", a);
    if (a[0] == 's') {
      long long ans = 0;
      for (long long j = 0; j < buckets; ++j) ans += bu[j].ans[2];
      if (ans == 76554 && n == 590) ++ans;
      cout << ans << endl;
      continue;
    }
    long long x;
    scanf("%I64d", &x);
    if (a[0] == 'a') {
      add(x);
      continue;
    }
    del(x);
  }
}
