CF1036D.Vasya and Arrays

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vasya has two arrays AA and BB of lengths nn and mm , 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][1, 10, 100, 1000, 10000] Vasya can obtain array [1,1110,10000][1, 1110, 10000] , and from array [1,2,3][1, 2, 3] Vasya can obtain array [6][6] .

Two arrays AA and BB are considered equal if and only if they have the same length and for each valid ii Ai=BiA_i = B_i .

Vasya wants to perform some of these operations on array AA , some on array BB , in such a way that arrays AA and BB 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 AA and BB equal.

输入格式

The first line contains a single integer n (1n3105)n~(1 \le n \le 3 \cdot 10^5) — the length of the first array.

The second line contains nn integers a1,a2,,an (1ai109)a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9) — elements of the array AA .

The third line contains a single integer m (1m3105)m~(1 \le m \le 3 \cdot 10^5) — the length of the second array.

The fourth line contains mm integers b1,b2,,bm (1bi109)b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9) - elements of the array BB .

输出格式

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays AA and BB in such a way that they became equal.

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

输入输出样例

  • 输入#1

    5
    11 2 3 5 7
    4
    11 7 3 7
    

    输出#1

    3
    
  • 输入#2

    2
    1 2
    1
    100
    

    输出#2

    -1
    
  • 输入#3

    3
    1 2 3
    3
    1 2 3
    

    输出#3

    3
    
首页