Under construction

Still being built. Things may change

Sums of Numbers 8

Interview

Time limit1sMemory limit512 MB

Summary
Count the index pairs (i, j) where the sum of A over the range [i, j] equals the sum of B over the same range.
Level

Easy3 of 10

Topics
Prefix sum, Hash map, Array
Solved
No attempts yet

Problem

Given two integer sequences of the same length, A={A1,A2,…,AN}A = \{A_1, A_2, \ldots, A_N\} and B={B1,B2,…,BN}B = \{B_1, B_2, \ldots, B_N\}.

Find the number of pairs of positive integers (i,j)(i, j) (i≤ji \leq j) that satisfy Ai+Ai+1+⋯+Aj=Bi+Bi+1+⋯+BjA_i + A_{i+1} + \cdots + A_j = B_i + B_{i+1} + \cdots + B_j.

Input

The first line contains a positive integer NN.

The second line contains NN positive integers AiA_i in order from A1A_1 to ANA_N, separated by spaces.

The third line contains NN positive integers BiB_i in order from B1B_1 to BNB_N, separated by spaces.

Output

Print the number of pairs of positive integers (i,j)(i, j) (i≤ji \leq j) that satisfy Ai+Ai+1+⋯+Aj=Bi+Bi+1+⋯+BjA_i + A_{i+1} + \cdots + A_j = B_i + B_{i+1} + \cdots + B_j.

Constraints

  • 1≤N≤2⋅1051 \leq N \leq 2\cdot 10^5
  • ∣A∣=∣B∣=N\lvert A \rvert = \lvert B \rvert = N
  • 1≤Ai,Bi≤1041 \leq A_i,B_i \leq 10^4

Hint

The answer can exceed the range of a 32-bit integer type, so use an integer type that fits your language.

  • Java: long, Long
  • Kotlin: Long
  • Swift: Int64
  • C, C++: long, long long

Examples4

  1. Example 1

    Input
    3
    1 2 3
    1 3 2
    
    Expected output
    3
    
  2. Example 2

    Input
    5
    1 2 3 4 5
    4 5 6 7 8
    
    Expected output
    0
    
  3. Example 3

    Input
    6
    23 13 31 17 29 19
    23 13 31 17 29 19
    
    Expected output
    21
    
  4. Example 4

    Input
    3
    1 2 1
    1 1 1
    
    Expected output
    2