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

#BS0000282. Làm bánh trong thời gian ngắn nhất (Roti Prata)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Làm bánh trong thời gian ngắn nhất (Roti Prata)

Roti Prata

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Prepare at least PP pratas using LL cooks working concurrently. Cook i has rank RiR_i and needs jRijR_i minutes for their j-th prata, with no overlap between their own tasks. Find the minimum integer completion time.

Input

First line: PP. Next line: LL followed by LL ranks RiR_i.

Output

Print the minimum time in minutes.

Subtasks

  • Subtask 1 (20 points): 1≤P≤301\le P\le30; 1≤L≤81\le L\le8; 1≤Ri≤81\le R_i\le8 (1≤i≤L1\le i\le L).
  • Subtask 2 (30 points): 1≤P≤10001\le P\le1000; 1≤L≤501\le L\le50; 1≤Ri≤81\le R_i\le8; R1=⋯=RLR_1=\cdots=R_L.
  • Subtask 3 (50 points): 1≤P≤10001\le P\le1000; 1≤L≤501\le L\le50; 1≤Ri≤81\le R_i\le8 (1≤i≤L1\le i\le L).

Examples

Input 1

10
4 1 2 3 4

Output 1

12

Explanation

At time 12, capacities are 4,3,2,2; at time 11 they are 4,2,2,1.