http://codeforces.com/problemset/problem/950/B
Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.
Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.
Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.
You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.
The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.
It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.
Output
Print the maximum number of files the intercepted array could consist of.
Examples
input
Copy
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
output
3
input
Copy
3 3
1 10 100
1 100 10
output
2
input
Copy
1 4
4
1 1 1 1
output
1
Note
In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1and 4 + 4 = 8.
In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.
In the third example the only possibility is that the archive contains a single file of size 4.
Google翻译:
黑客Zhorik想破译他昨天截获的两条秘密消息。消息是一系列加密块,每个都由几个字节的信息组成。
Zhorik知道每个消息都是一个包含一个或多个文件的存档。 Zhorik知道每个档案是如何通过网络传输的:如果档案由大小为l1,l2,...,lk字节的k个文件组成,那么第i个文件被分成一个或多个区块bi,1, bi,2,...,bi,mi(这里块bi,1 + bi,2 + ... + bi,mi的总长度等于文件li的长度),并且之后所有块通过网络传输,维护档案文件的顺序。
Zhorik认为这两条消息包含相同的存档,因为它们的总长度是相等的。但是,每个文件可以以两种消息中的不同方式分块。
两个消息中的每一个都给出了块的长度。如果Zhorik的假设是正确的,请帮助Zhorik确定归档文件的最大数量。
要求x1 + ... + xn = y1 + ... + ym ,即x数组中的一段之和等于y数组中的一段之,不过要从左到右依次来。
用贪心来求,一个变量si保存x数组一段的和,另一个变量sj保存y数组一段的和,如果si<sj,si加上x数组中的下一个。反之sj加上y数组中的下一个。指导si==sj
#include<bits/stdc++.h>
using namespace std;
int x[100005],y[100005];
int main()
{
int n,m,i,j,si,sj;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%d",&x[i]);
for(i=1;i<=m;i++)
scanf("%d",&y[i]); //输入
int ans=0;
i=j=1; //下标
si=x[1];
sj=y[1];
while(i<=n&&j<=m) //x、y数组中还有数字
{
if(si==sj){ //相等si、sj都变成下一个
ans++;
i++;
j++;
si=x[i];
sj=y[j];
}else{
if(si<sj){
i++; //si加下一个
si+=x[i];
}else{
j++; //sj加下一个
sj+=y[j];
}
}
}
printf("%d",ans);
return 0;
}