Simple and hard to write:
For I: = 0 to n do f [I, 0]: = 0; for I: = 1 to n do begin for J: = 1 to a [I]. s-1 do f [I, j]: = max (F [I-1, J], F [I, J-1]); For J: = A [I]. s to a [I]. S + A [I]. l-1 do begin if j> L then break; F [I, j]: = max (F [I, J-1], F [I-1, J]); For K: = 0 to a [I]. s do if J-k <= A [I]. l then f [I, j]: = max (F [I, j], F [I-1, K] + (J-k) * A [I]. p); if f [I, j]> ans then ans: = f [I, j]; end;
I wrote it for more than an hour, but I didn't pay attention to the continuity at first. Later I forgot the initialization of the value before a [I]. S.
A very good question.
Give the kernel after monotonous queue OptimizationCode:
Begin readln (L, n); for I: = 1 to n do with a [I] Do readln (L, P, S); QS (1, N ); for I: = 1 to n do begin for J: = 0 to a [I]. s-1 do f [I, j]: = f [I-1, J]; head: = 1; tail: = 1; W [1]: =-1; Q [1]: = 0; for J: = max (0, a [I]. s-A [I]. l) to a [I]. s-1 do begin K: = f [I-1, J]-A [I]. p * j; while (Head <= tail) and (w [tail] <= k) Do Dec (tail); Inc (tail); W [tail]: = K; Q [tail]: = J; end; for J: = A [I]. s to l do begin while (Head <= tail) and (Q [head] <j-A [I]. l) Do Inc (head); F [I, j]: = max (F [I-1, J], F [I, J-1]); if head <= tail then f [I, j]: = max (F [I, j], W [head] + A [I]. p * j); end; writeln (F [n, l]); end.