All problems

Problem 573

Pairs of Songs With Total Durations Divisible by 60

Medium · 20 pts · Array, Hash Table, Counting

Problem

You are given the durations of N songs in seconds. Count the pairs of songs i < j whose total duration is divisible by 60. Checking every pair is O(N²). Instead, count how many songs have each remainder modulo 60; a song with remainder r pairs with every earlier song whose remainder is (60 - r) mod 60. The answer can exceed 2^31, so use a 64-bit integer.

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 pairs whose total is divisible by 60.

Constraints

1 <= N <= 100000; 1 <= A[i] <= 500

Example input

5
30 20 150 100 40

Example output

3

Explanation

For 30 20 150 100 40, the pairs (30, 150), (20, 100) and (20, 40) have totals 180, 120 and 60. Each is divisible by 60, and no other pair is, so the answer is 3.

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

Previous: Largest Perimeter Triangle Next: Successful Pairs of Spells and Potions