CF Educational Codeforces Round 50 (Rated for Div. 2) D. Vasya and Arrays ʕ •ᴥ•ʔ

D. Vasya and Arrays

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya has two arrays A

and B of lengths n and m

, respectively.

He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1,10,100,1000,10000]

Vasya can obtain array [1,1110,10000], and from array [1,2,3] Vasya can obtain array [6]

.

Two arrays A

and B are considered equal if and only if they have the same length and for each valid i Ai=Bi

.

Vasya wants to perform some of these operations on array A

, some on array B, in such a way that arrays A and B

become equal. Moreover, the lengths of the resulting arrays should be maximal possible.

Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays A

and B

equal.

Input

The first line contains a single integer n (1≤n≤3⋅105)

— the length of the first array.

The second line contains n

integers a1,a2,⋯,an (1≤ai≤109) — elements of the array A

.

The third line contains a single integer m (1≤m≤3⋅105)

— the length of the second array.

The fourth line contains m

integers b1,b2,⋯,bm (1≤bi≤109) - elements of the array B

.

Output

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays A

and B

in such a way that they became equal.

If there is no way to make array equal, print "-1".

Examples

Input

Copy

5
11 2 3 5 7
4
11 7 3 7

Output

Copy

3

Input

Copy

2
1 2
1
100

Output

Copy

-1

Input

Copy

3
1 2 3
3
1 2 3

Output

Copy

3

题意:给你两个字符串 问是否可以转换(相邻相加)成两个相同的字符串 如果不可以输出 -1 如果可以问最大长度是多少

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int a[300010];
int b[300010];
int main()
{
	int n,m;
	scanf("%d",&n);
	ll h1=0,h2=0;
	int l=0;
	for(int i=1;i<=n;i++)
	scanf("%d",&a[i]),h1+=a[i];
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	scanf("%d",&b[i]),h2+=b[i];
	if(h1!=h2)
	{
	printf("-1\n");
	return 0;
	}
	ll sa=0,sb=0;
	int j=1;
	for(int i=1;i<=n;i++)
	{
		sa+=a[i];
		while(sa>sb)
		{
			sb+=b[j];
			j++;

		}
		if(sa==sb)
			l++;
	}
	printf("%d\n",l);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值