All problems

Problem 977

Minimum Rounds to Complete All Tasks

Medium · 20 pts · Array, Greedy, Hash Table, Counting

Problem

Each task has a difficulty level. In one round you complete either 2 or 3 tasks of the same difficulty. Find the minimum number of rounds to complete all tasks, or -1 if it is impossible. Count the tasks of each difficulty: a count of 1 can never be finished, and any other count c takes ceil(c / 3) rounds, because groups of 3 are best and a remainder of 1 or 2 can be absorbed with one or two groups of 2.

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 minimum number of rounds, or -1.

Constraints

1 <= N <= 100000; 1 <= A[i] <= 10^9

Example input

10
2 2 3 3 2 4 4 4 4 4

Example output

4

Explanation

For 2 2 3 3 2 4 4 4 4 4, the three 2s take 1 round, the two 3s take 1 round, and the five 4s take 2 rounds (3 + 2). That is 4 rounds.

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

Previous: Maximum Bags With Full Capacity of Rocks Next: Eliminate Maximum Number of Monsters