cf 512 (div.2) C. Vasya and Golden Ticket

Recently Vasya found a golden ticket — a sequence which consists of nn digits a1a2…ana1a2…an. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket 350178350178 is lucky since it can be divided into three segments 350350, 1717 and 88: 3+5+0=1+7=83+5+0=1+7=8. Note that each digit of sequence should belong to exactly one segment.

Help Vasya! Tell him if the golden ticket he found is lucky or not.

Input

The first line contains one integer nn (2≤n≤1002≤n≤100) — the number of digits in the ticket.

The second line contains nn digits a1a2…ana1a2…an (0≤ai≤90≤ai≤9) — the golden ticket. Digits are printed without spaces.

Output

If the golden ticket is lucky then print "YES", otherwise print "NO" (both case insensitive).

Examples

input

Copy

5
73452

output

扫描二维码关注公众号,回复: 3448149 查看本文章

Copy

YES

input

Copy

4
1248

output

Copy

NO

Note

In the first example the ticket can be divided into 77, 3434 and 5252: 7=3+4=5+27=3+4=5+2.

In the second example it is impossible to divide ticket into segments with equal sum.

给一个长度为n的序列,问能否拆成两个或两个以上的序列,使得这些序列的和相等

女娲补天过.

#pragma GCC optimize(2)
#include<stdio.h>
#include<algorithm>
#include<bits/stdc++.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
typedef long long ll;
char str[maxn];
int main()
{
	//freopen("C://input.txt", "r", stdin);
	int flag = 0;
	int n, sum;
	int sum1;
	sum1 = 0;
	int cnt;
	scanf("%d", &n);
	cin >> str;
	for (int i = 0; i < n - 1; i++)
	{
		sum = 0;
		cnt = 0;
		sum1 = 0;
		for (int j = 0; j <= i; j++)
		{
			sum += str[j] - '0';
		}
		for (int j = i+1; j < n; j++)
		{
			sum1 += str[j] - '0';
			if (sum1 > sum)
			{
				sum1 = 0;
				flag = 0;
				cnt++;
				break;
			}
			if (j == n - 1)
			{
				if (sum1 < sum)
				{
					flag = 0;
				}
			}
			if (sum1 < sum)
			{
				continue;
			}
			if (sum1 == sum)
			{
				if (str[j + 1] == '0')
				{
					continue;
				}
				else
				{
					sum1 = 0;
					flag = 1;
				}
			}
		}
		if (cnt == 0 && flag)
		{
			printf("YES\n");
			return 0;
		}
	}
	printf("NO\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Evildoer_llc/article/details/82825909