您的位置:首页 > 其它

Codeforces Round #469 (Div. 2) B. Intercepted Message

2018-03-11 15:17 288 查看
题目链接:点击打开链接

B. Intercepted Message

time limit per test
1 second

memory limit per test
512 megabytes

input
standard input

output
standard output

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 31 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 + 1 and 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.

题意:有两串数字,若第一段数字中的一些连续数字的和 和 第二串数字中的一些连续数字的和相等。或者第二段中的一些数字和等于第一段数字和。那么算解密出一段密码。问能解密密码数的最大值。

题解:求两个前缀和,然后暴力匹配一边看有几个相等的数字。就是结果。因为是数字必须连续。所以可以前缀和求解。但是暴力匹配会时间超限。所以需要把前缀和的结果用数组记录一下(类似于桶排序)。

详细实现过程见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
const int maxN = 1e6+5;
int book[maxN];
int main(){
int a[maxn],b[maxn];
int qa[maxn],qb[maxn];
int n,m;
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++){
scanf("%d",&a[i]);
qa[i] = qa[i-1]+a[i];
book[qa[i]]++;
}
for(int j = 1 ; j <= m ; j ++){
scanf("%d",&b[j]);
qb[j] = qb[j-1] + b[j];
}
int ans = 0;
for(int i = 1 ; i <= m ; i ++){
if(book[qb[i]]){
ans ++;
book[qb[i]] --;
}
}
cout << ans << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: