Under construction

Still being built. Things may change

Speed Reading

Interview

Time limit1sMemory limit128 MB

Summary
For each cow, simulate reading in bursts of at most T minutes with R-minute rests between, and report the total minutes rounded up to finish N pages.
Level

Medium4 of 10

Topics
Simulation, Implementation, Math, Greedy
Solved
No attempts yet

Problem

All KK (1≤K≤1,0001 \le K \le 1{,}000) cows are taking part in Farmer John's annual reading contest. The contest is to read a single book of NN (1≤N≤100,0001 \le N \le 100{,}000) pages as fast as possible while understanding it.

Cow ii has a reading speed of SiS_i (1≤Si≤1001 \le S_i \le 100) pages per minute, a maximum continuous reading time of TiT_i (1≤Ti≤1001 \le T_i \le 100) minutes, and a minimum rest time of RiR_i (1≤Ri≤1001 \le R_i \le 100) minutes. The cow reads SiS_i pages per minute, but she can read for at most TiT_i minutes at a stretch. Once she stops to rest, she must rest for RiR_i minutes before she can start reading again.

For each cow, determine the number of minutes (rounded up to the next whole minute) needed to read the entire book.

Input

  • Line 1: two space-separated integers NN and KK.
  • Lines 2 to K+1K+1: line i+1i+1 contains three space-separated integers SiS_i, TiT_i, and RiR_i.

Output

  • Lines 1 to KK: line ii is the number of minutes (rounded up to the next whole minute) that cow ii needs to read the whole book.

Hint

Reading always happens in whole-minute units. A cow reads in bursts of at most TiT_i minutes (covering at most Si⋅TiS_i \cdot T_i pages); if pages still remain afterward she rests RiR_i minutes and reads again. No rest is needed after the final burst that finishes the book, and a partial last minute still counts as a full minute.

For example, for a 10-page book, a cow that reads 2 pages/min for up to 4 minutes and rests 1 minute reads 8 pages in 4 minutes, rests 1 minute, then reads the last 2 pages in 1 minute, for 6 minutes total.

Examples1

  1. Example 1

    Input
    10 3
    2 4 1
    6 1 5
    3 3 3
    
    Expected output
    6
    7
    7