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

#BS0000279. Thổi bóng cho ngày hội thiếu nhi (Children Holiday)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thổi bóng cho ngày hội thiếu nhi (Children Holiday)

Children Holiday

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

The organizers need exactly mm inflated balloons. There are nn helpers working concurrently. Helper i takes tit_i minutes per balloon and must rest for yiy_i minutes after every batch of ziz_i balloons before continuing. A rest after the last assigned balloon does not count. Find the shortest completion time and any optimal assignment of balloons to helpers.

Input

First line: m,nm,n. Each of the next n lines contains ti,zi,yit_i,z_i,y_i.

Output

Print the minimum time TT on the first line. On the second line, print nn nonnegative integers b1,…,bnb_1,\ldots,b_n summing to m; helper i must finish bib_i balloons within T minutes. Any optimal assignment is accepted.

Subtasks

  • Subtask 1 (20 points): 0≤m≤300\le m\le30; 1≤n≤81\le n\le8; 1≤ti,yi≤101\le t_i,y_i\le10; 1≤zi≤101\le z_i\le10 (1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 0≤m≤150000\le m\le15000; 1≤n≤10001\le n\le1000; 1≤ti,yi≤1001\le t_i,y_i\le100; 1≤zi≤10001\le z_i\le1000 (1≤i≤n1\le i\le n); mọi bộ (ti,zi,yi)(t_i,z_i,y_i) bằng nhau.
  • Subtask 3 (50 points): 0≤m≤150000\le m\le15000; 1≤n≤10001\le n\le1000; 1≤ti,yi≤1001\le t_i,y_i\le100; 1≤zi≤10001\le z_i\le1000 (1≤i≤n1\le i\le n).

Examples

Input 1

4 2
1 2 3
2 3 1

Output 1

4
2 2

Explanation

In four minutes, the first helper makes two balloons and rests, while the second makes two balloons. In three minutes, the combined capacity is only 2+1=3.