code:
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
#define int long long
const ll mod = 1000000007;
const ll maxn = 100055;
struct node {
ll l, r;
mutable ll v;
node(ll L, ll R = 0, ll V = 0) : l(L), r(R), v(V) {}
bool operator<(const node& a)const {
return l < a.l;
}
};
set<node>odt;
#define IT set<node>::iterator
set<node>::iterator split(int pos) {
set<node>::iterator it = odt.lower_bound(node(pos));
if (it != odt.end() && it->l == pos)return it;
it--;
if (it->r < pos)return odt.end();
ll l = it->l;
ll r = it->r;
ll v = it->v;
odt.erase(it);
odt.insert(node(l, pos - 1, v));
return odt.insert(node(pos, r, v)).first;
}
void add(ll l, ll r, ll x) {
set<node>::iterator itr = split(r + 1), itl = split(l);
for (set<node>::iterator it = itl; it != itr; it++)it->v += x;
}
void assign(ll l, ll r, ll x) {
set<node>::iterator itr = split(r + 1), itl = split(l);
odt.erase(itl, itr);
odt.insert(node(l, r, x));
}
void oper(ll l,ll r){
set<node>::iterator itr = split(r + 1), itl = split(l);
for(set<node>::iterator it=itl;it!=itr;++it){
if(it -> v != 2) (it -> v) += 1;
}
}
signed main(){
odt.insert(node(-1e9 - 6, 1e9 + 6, 0));
int n;
char op;
int k, cur = 0;
for(int i = 1; i <= n; i++) {
cin >> k >> op;
if(op == 'L')k = -k;
int l = cur, r = cur + k;
if(l>r)swap(l, r);
oper(l, r);
}
int ans=0;
for(set<node>::iterator it=odt.begin();it!=odt.end();it++){
if(it->v == 2)ans += it->r - it->l + 1;
}
cout<<ans<<endl;
return 0;
}