#P1704. 寻找最优美做题曲线

寻找最优美做题曲线

Description

Luogu OJ has a fun judging feature that automatically draws a user's "problem-solving curve." This curve is a polyline defined as follows: suppose a user solved cbic_{b_i} problems on day bib_i, where bib_i are strictly increasing. Then the user's problem-solving curve is the polyline passing through the points (i,cbi)(i, c_{b_i}) in order. For example, if you solved 33 problems on day 11, 44 on day 33, and 11 on day 66, then your problem-solving curve over the first 66 days is the continuous polyline from (1,3)(1, 3) to (2,4)(2, 4) to (3,1)(3, 1).

nodgd can predict how many problems he would be able to AC on each of the next NN days. He also has an obsession with being strictly increasing, so he forces his problem-solving curve to be strictly increasing. For some reasons, on certain days (a total of KK days), he must solve problems, and the number of problems solved on those days must exactly match the predicted numbers (showcasing nodgd’s divine prediction). He wants to know, under these constraints, the maximum number of days on which he can solve problems. However, since he still has a pile of math, physics, English, art, and PE competition problems to do, he is asking you to compute it for him.

Input Format

  • The first line contains two positive integers NN and KK, meaning nodgd has predictions for the next NN days, among which KK days are mandatory.
  • The second line contains KK distinct positive integers pip_i, indicating that day pip_i is mandatory (the pip_i are not guaranteed to be in increasing order).
  • The third line contains NN positive integers cic_i, where on day ii, if nodgd solves problems, the number must be exactly cic_i.

Output Format

Output a single line.

  • If a strictly increasing problem-solving curve can be constructed, output a single positive integer: the maximum number of days on which nodgd can solve problems.
  • If no strictly increasing curve exists, output impossible.
13 4
2 13 8 7
6 10 9 8 9 10 11 16 14 12 13 14 18 
5

Hint

Constraints

For all testdata,

  • 1N5000001 \le N \le 500000, 1KN/21 \le K \le N/2
  • 1piN1 \le p_i \le N, and all pip_i are distinct; the pip_i are not guaranteed to be in increasing order;
  • 1ci1091 \le c_i \le 10^9

Translated by ChatGPT 5