All problems

Problem 952

Arithmetic Slices

Medium · 20 pts · Array, Dynamic Programming

Problem

A block of consecutive elements is arithmetic if it has at least three elements and every two neighbours in it have the same difference, like 1 3 5 7. Count the arithmetic blocks of the array (counted by position). If the arithmetic run ending at index i has length L >= 3, it ends L - 2 arithmetic blocks there, so keep a running count of how many arithmetic blocks end at each index and add them up.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the number of arithmetic subarrays.

Constraints

1 <= N <= 5000; -1000 <= A[i] <= 1000

Example input

4
1 2 3 4

Example output

3

Explanation

In 1 2 3 4, the arithmetic blocks are 1 2 3, 2 3 4 and 1 2 3 4, so the answer is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Decode Ways II Next: Maximum Length of Pair Chain