CodeForces 950B Intercepted Message

Problem Description

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

7 6
2 5 3 1 11 4 4
7 8 2 4 1 8

output

3

input

3 3
1 10 100
1 100 10

output

2

input

1 4
4
1 1 1 1

output

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 <algorithm>
#include <cmath>
using namespace std;

int main() {
    int m, n;
    cin >> m >> n;
    int a[m], b[n];
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
    }
    for (int j = 0; j < n; ++j) {
        cin >> b[j];
    }
    int i = 0, j = 0, ans = 0;
    int sum1 = a[i], sum2 = b[j];
    while(i < m && j < n){
        if(sum1 < sum2){
            i++;
            sum1 += a[i];

        }
        if (sum1 > sum2){
            j++;
            sum2 += b[j];
        }
        if(sum1 == sum2){
            ans ++;
            i++;
            j++;
            sum1 = a[i];
            sum2 = b[j];
        }

    }
    cout << ans << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值