#include <bits/stdc++.h>
#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define x first
#define y second
#define less(a,b) ((a) < (b) - EPS)
#define more(a,b) ((a) > (b) + EPS)
#define eq(a,b) (fabs((a) - (b)) < EPS)
#define remax(a, b) ((a) = (b) > (a) ? (b) : (a))
#define remin(a, b) ((a) = (b) < (a) ? (b) : (a))
using namespace std;
typedef long long ll;
struct node
{
ll cost;
ll cnt;
node * to[2];
node()
{
cost = 0, cnt = 0;
to[0] = 0, to[1] = 0;
}
};
const int digits_number = 32;
inline unsigned rev(unsigned v)
{
unsigned r = 0;
for(int i = 0; i < digits_number; ++i)
{
r |= (v & 1) << (digits_number - 1 - i);
v >>= 1;
}
return r;
}
void add(node * root, unsigned cost, ll cnt)
{
unsigned rev_cost = rev(cost);
ll add_cost = cnt * cost;
int it = digits_number;
while(it--)
{
root->cnt += cnt;
root->cost += add_cost;
if (!root->to[rev_cost & 1])
root->to[rev_cost & 1] = new node();
root = root->to[rev_cost & 1];
rev_cost >>= 1;
}
root->cost += add_cost;
root->cnt += cnt;
}
inline ll Cnt(node * z)
{
return z ? z->cnt : 0;
}
inline ll Cost(node * z)
{
return z ? z->cost : 0;
}
ll GET(node * sell, node * buy)
{
ll ans = 0;
ll sell_count = 0, buy_count = 0;
int it = digits_number;
unsigned cost = 0;
while(it--)
{
if (sell_count + Cnt(sell->to[0]) == buy_count + Cnt(buy->to[1]))
{
ans -= Cost(sell->to[0]) - Cost(buy->to[1]);
return ans;
}
if (sell_count + Cnt(sell->to[0]) < buy_count + Cnt(buy->to[1]))
{
ans -= Cost(sell->to[0]);
sell_count += Cnt(sell->to[0]);
sell = sell->to[1];
buy = buy->to[1];
cost = cost * 2 + 1;
}
else
{
ans += Cost(buy->to[1]);
buy_count += Cnt(buy->to[1]);
sell = sell->to[0];
buy = buy->to[0];
cost = cost * 2;
}
}
ans += (sell_count - buy_count) * cost;
return ans;
}
struct query
{
int type;
unsigned cost;
ll cnt;
};
query nextQuery()
{
string type;
cin >> type;
query res;
if (type[0] == 'e')
exit(0);
if (type[0] == 's')
res.type = 0;
else
res.type = 1;
cin >> res.cnt >> res.cost;
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
node * sell = new node();
node * buy = new node();
while(1)
{
query Q = nextQuery();
if (Q.type == 0)
{
add(sell, Q.cost, Q.cnt);
add(buy, Q.cost, 0);
}
else
{
add(buy, Q.cost, Q.cnt);
add(sell, Q.cost, 0);
}
cout << GET(sell, buy) << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgY2xyKHgpIG1lbXNldCgoeCksIDAsIHNpemVvZih4KSkKI2RlZmluZSBhbGwoeCkgKHgpLmJlZ2luKCksICh4KS5lbmQoKQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIEZvcihpLCBzdCwgZW4pIGZvcihpbnQgaT0oc3QpOyBpPD0oaW50KShlbik7IGkrKykKI2RlZmluZSBGb3JkKGksIHN0LCBlbikgZm9yKGludCBpPShzdCk7IGk+PShpbnQpKGVuKTsgaS0tKQojZGVmaW5lIGZvcm4oaSwgbikgZm9yKGludCBpPTA7IGk8KGludCkobik7IGkrKykKI2RlZmluZSBmb3JkKGksIG4pIGZvcihpbnQgaT0obiktMTsgaT49MDsgaS0tKQojZGVmaW5lIGZvcmkoaXQsIHgpIGZvciAoX190eXBlb2YoKHgpLmJlZ2luKCkpIGl0ID0gKHgpLmJlZ2luKCk7IGl0ICE9ICh4KS5lbmQoKTsgaXQrKykKI2RlZmluZS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pbGUoaXQtLSkKICAgIHsKICAgICAgICByb290LT5jbnQgKz0gY250OwogICAgICAgIHJvb3QtPmNvc3QgKz0gYWRkX2Nvc3Q7CiAgICAgICAgaWYgKCFyb290LT50b1tyZXZfY29zdCAmIDFdKQogICAgICAgICAgICByb290LT50b1tyZXZfY29zdCAmIDFdID0gbmV3IG5vZGUoKTsKICAgICAgICByb290ID0gcm9vdC0+dG9bcmV2X2Nvc3QgJiAxXTsKICAgICAgICByZXZfY29zdCA+Pj0gMTsKICAgIH0KICAgIHJvb3QtPmNvc3QgKz0gYWRkX2Nvc3Q7CiAgICByb290LT5jbnQgKz0gY250Owp9CgppbmxpbmUgbGwgQ250KG5vZGUgKiB6KQp7CiAgICByZXR1cm4geiA/IHotPmNudCA6IDA7Cn0KCmlubGluZSBsbCBDb3N0KG5vZGUgKiB6KQp7CiAgICByZXR1cm4geiA/IHotPmNvc3QgOiAwOwp9CgpsbCBHRVQobm9kZSAqIHNlbGwsIG5vZGUgKiBidXkpCnsKICAgIGxsIGFucyA9IDA7CiAgICBsbCBzZWxsX2NvdW50ID0gMCwgYnV5X2NvdW50ID0gMDsKICAgIGludCBpdCA9IGRpZ2l0c19udW1iZXI7CiAgICB1bnNpZ25lZCBjb3N0ID0gMDsKICAgIHdoaWxlKGl0LS0pCiAgICB7CiAgICAgICAgaWYgKHNlbGxfY291bnQgKyBDbnQoc2VsbC0+dG9bMF0pID09IGJ1eV9jb3VudCArIENudChidXktPnRvWzFdKSkKICAgICAgICB7CiAgICAgICAgICAgIGFucyAtPSBDb3N0KHNlbGwtPnRvWzBdKSAtIENvc3QoYnV5LT50b1sxXSk7CiAgICAgICAgICAgIHJldHVybiBhbnM7CiAgICAgICAgfQogICAgICAgIGlmIChzZWxsX2NvdW50ICsgQ250KHNlbGwtPnRvWzBdKSA8IGJ1eV9jb3VudCArIENudChidXktPnRvWzFdKSkKICAgICAgICB7CiAgICAgICAgICAgIGFucyAtPSBDb3N0KHNlbGwtPnRvWzBdKTsKICAgICAgICAgICAgc2VsbF9jb3VudCArPSBDbnQoc2VsbC0+dG9bMF0pOwogICAgICAgICAgICBzZWxsID0gc2VsbC0+dG9bMV07CiAgICAgICAgICAgIGJ1eSA9IGJ1eS0+dG9bMV07CiAgICAgICAgICAgIGNvc3QgPSBjb3N0ICogMiArIDE7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGFucyArPSBDb3N0KGJ1eS0+dG9bMV0pOwogICAgICAgICAgICBidXlfY291bnQgKz0gQ250KGJ1eS0+dG9bMV0pOwogICAgICAgICAgICBzZWxsID0gc2VsbC0+dG9bMF07CiAgICAgICAgICAgIGJ1eSA9IGJ1eS0+dG9bMF07CiAgICAgICAgICAgIGNvc3QgPSBjb3N0ICogMjsKICAgICAgICB9CiAgICB9CiAgICBhbnMgKz0gKHNlbGxfY291bnQgLSBidXlfY291bnQpICogY29zdDsKICAgIHJldHVybiBhbnM7Cn0KCnN0cnVjdCBxdWVyeQp7CiAgICBpbnQgdHlwZTsKICAgIHVuc2lnbmVkIGNvc3Q7CiAgICBsbCBjbnQ7Cn07CgpxdWVyeSBuZXh0UXVlcnkoKQp7CiAgICBzdHJpbmcgdHlwZTsKICAgIGNpbiA+PiB0eXBlOwogICAgcXVlcnkgcmVzOwogICAgaWYgKHR5cGVbMF0gPT0gJ2UnKQogICAgICAgIGV4aXQoMCk7CiAgICBpZiAodHlwZVswXSA9PSAncycpCiAgICAgICAgcmVzLnR5cGUgPSAwOwogICAgZWxzZQogICAgICAgIHJlcy50eXBlID0gMTsKICAgIGNpbiA+PiByZXMuY250ID4+IHJlcy5jb3N0OwogICAgcmV0dXJuIHJlczsKfQoKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKCiAgICBub2RlICogc2VsbCA9IG5ldyBub2RlKCk7CiAgICBub2RlICogYnV5ID0gbmV3IG5vZGUoKTsKCiAgICB3aGlsZSgxKQogICAgewogICAgICAgIHF1ZXJ5IFEgPSBuZXh0UXVlcnkoKTsKICAgICAgICBpZiAoUS50eXBlID09IDApCiAgICAgICAgewogICAgICAgICAgICBhZGQoc2VsbCwgUS5jb3N0LCBRLmNudCk7CiAgICAgICAgICAgIGFkZChidXksIFEuY29zdCwgMCk7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGFkZChidXksIFEuY29zdCwgUS5jbnQpOwogICAgICAgICAgICBhZGQoc2VsbCwgUS5jb3N0LCAwKTsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBHRVQoc2VsbCwgYnV5KSA8PCBlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0K