PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000121. Tổng các đoạn có độ dài lẻ (Sum of All Odd Length Subarrays)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng các đoạn có độ dài lẻ (Sum of All Odd Length Subarrays)

Sum of All Odd Length Subarrays

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}, consider every interval [l,r][l,r] with 0≤l≤r<n0\le l\le r<n and odd length r−l+1r-l+1. Add the sum of each such contiguous interval to a single total. Intervals with different endpoints count separately even if their values coincide.

Input

The first line contains nn. The second line contains the nn integers a0,…,an−1a_0,\ldots,a_{n-1}.

Output

Print the required integer total.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; 1≤ai≤10001\le a_i\le 1000 for 0≤i<n0\le i<n.
  • Subtask 2 (30 points): 1≤n≤1001\le n\le 100; ai=1a_i=1 for 0≤i<n0\le i<n.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le 100; 1≤ai≤10001\le a_i\le 1000 for 0≤i<n0\le i<n.

Example 1

Input

5
1 4 2 5 3

Output

58

Explanation

Length-one intervals contribute 15; the three length-three sums are 7, 11, 10; the length-five sum is 15. The total is 58.

Example 2

Input

2
1 2

Output

3

Explanation

Only the two singleton intervals qualify, giving 1 + 2 = 3.

Example 3

Input

3
10 11 12

Output

66

Explanation

Singletons contribute 33 and the length-three interval contributes 33, giving 66.