Japanese Puzzle

Time limit2sMemory limit64 MB

Summary
Given counts of k picture types filling an n by n grid, find the maximum number of rows that can be rearranged to be identical, using each picture's total count as a constraint.
Level

Medium6 of 10

Topics
Binary search, Math, Greedy
Solved
No attempts yet

Problem

A brand-new puzzle is arriving from the East, hoping to rival the world-famous Sudoku and become an international hit. Its exact rules are still a secret, but the objective has already been announced: you are given an n×nn \times n square grid in which every cell holds a block showing one of kk kinds of pictures, and you must rearrange the blocks so that as many rows as possible become identical to one another. Two rows count as identical when they contain the same pictures in the same order.

Rearranging only moves the existing blocks around: the whole collection of pictures is preserved, so no picture is ever added and none is removed.

Andy works at a puzzle review magazine and became curious about the news. He realized that the information known so far is already enough to determine how many rows can be made identical in the best possible arrangement, and he wants a program that computes this number for any starting configuration.

For example, a puzzle that starts out like this

can be rearranged so that several top rows repeat, as in

Input

The first line contains two integers nn and kk (1≤n≤400001 \le n \le 40000, 1≤k≤500001 \le k \le 50000). Each of the next kk lines contains one integer lil_i (li>0l_i > 0): the number of blocks that show the ii-th kind of picture. It is guaranteed that ∑i=1kli=n2\sum_{i=1}^{k} l_i = n^2.

Output

Output a single integer: the maximum number of rows that can be made identical to one another.

Examples3

  1. Example 1

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

    Input
    1 1
    1
    
    Expected output
    1
    
  3. Example 3

    Input
    3 2
    5
    4
    
    Expected output
    2