You are given a function 232−1. The function contains three types of commands:

  • for n — for loop;
  • end — every command between "for ntimes;
  • add — adds 1 to x.

After the execution of these commands, value of x is returned.

Every "for 

Notice that "add" commands might overflow the value of 

Now you run x is correct or some overflow made it incorrect.

If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of x.

Input

The first line contains a single integer 1≤l≤105) — the number of lines in the function.

Each of the next l lines contains a single command of one of three types:

  • for 1≤n≤100) — for loop;
  • end — every command between "for ntimes;
  • add — adds 1 to x.

Output

If overflow happened during execution of x.

Examples

Input
9
add
for 43
end
for 10
for 15
add
end
add
end
Output
161
Input
2
for 62
end
Output
0
Input
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
Output
OVERFLOW!!!

Note

In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for 

In the second example there are no commands "add", thus the returning value is 0.

In the third example "add" command is executed too many times, which causes 232−1.

 

 

题意:

给出for循环伪代码,计算执行add的次数(加了几次)。

思路:

很新颖的一道题,不难想到使用栈模拟。

因为栈底的值会影响栈顶的结果,因此放入累乘值来表示当前的状态,每次add只需加入栈顶的值即可。

具体实现见代码,注意判断越界。

 

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
 
string s;
stack<ll> st;
 
int main()
{
    int t,i;
    ll x=0,y=0;
    scanf("%d",&t);
    st.push(1);
    int f=0;
    while(t--){
        scanf(" ");
        cin>>s;
        if(s[0]=='f'){
            scanf("%I64d",&x);
            st.push(min(4294967296ll,x*st.top()));
        }
        else if(s[0]=='a'){
            y+=st.top();
            if(y>=4294967296){
                f=1;
                break;
            }
        }
        else if(s[0]=='e'){
            st.pop();
        }
    }
    if(f==1) printf("OVERFLOW!!!\n");
    else printf("%I64d\n",y);
    return 0;
}

相关文章: