You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1.
The decoding of the lever description is given below.
- If the i-th character of the string equals "^", that means that at coordinate i there is the pivot under the bar.
- If the i-th character of the string equals "=", that means that at coordinate i there is nothing lying on the bar.
- If the i-th character of the string equals digit c (1-9), that means that at coordinate i there is a weight of mass c on the bar.
Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.
The first line contains the lever description as a non-empty string s (3 ≤ |s| ≤ 106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar.
To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.
Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance", if it is in balance.
=^==
balance
9===^==1
left
2==^7==
right
41^52==
balance
As you solve the problem, you may find the following link useful to better understand how a lever functions:http://en.wikipedia.org/wiki/Lever.
The pictures to the examples:
![](https://i-blog.csdnimg.cn/blog_migrate/3dc40f378cd9c50a7c2ea216bec7134f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a7dc7811df161675a3ebd8562962dd92.png)
![](https://i-blog.csdnimg.cn/blog_migrate/75f5fd13adf0e58c57f0c822f1d38510.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ffe5ffeeb73167fe5f98be361fb661ab.png)
#include <stdio.h>
#include <string.h>
#define ll long long
#define rep(i,j,k) for(i=(j); i<k; i++)
#define maxn 1000010
int main(void){
char s[maxn];
scanf("%s",s);
getchar();
ll len = strlen(s);
ll i,key;
ll left = 0;
rep(i,0,len)
if(s[i] == '^')
key = i;
rep(i,0,key)
if(s[i]>'0' && s[i]<='9')
left += (s[i]-'0')*(key-i);
rep(i,key+1,len)
if(s[i]>'0' && s[i]<='9')
left -= (s[i]-'0')*(i-key);
if(left > 0)
printf("left\n");
else if(left < 0)
printf("right\n");
else printf("balance\n");
return 0;
}