The Strongest Teams for IKPC

Time limit: 1s Memory limit: 256MB Input: Output:

Task

The IKPC is right around the corner.
The kindergarten teacher, Theresa, wants to form some teams for the competition.
There are NN children in her class, and she wants to form exactly KK teams.

The children are sitting along a line, numbered from 00 to N1N-1, according to their position in the line.
Child ii has a team play factor AiA_i and a programming skill BiB_i.

Each team must have at least one member, and no child can be assigned to multiple teams.
Children 0i1<i2<<il<N0 \le i_1 < i_2 < \ldots < i_l < N (for some l>0l>0) can form a team if for each 1p<l1 \le p < l the following conditions hold:

  • Aip<Aip+1A_{i_p} < A_{i_{p+1}}, and
  • there is no child with index ip<x<ip+1i_p < x < i_{p+1} such that Aip<AxA_{i_p} < A_x.

The strength of a team formed this way is Bi1+Bi2++BilB_{i_1} + B_{i_2} + \ldots + B_{i_l}.

Your task is to find the maximum possible sum of team strengths that Theresa can achieve by forming exactly KK teams!

Input data

The input file consists of:

  • a line containing integers NN, KK.
  • a line containing the NN integers A0,,AN1A_{0}, \, \ldots, \, A_{N-1}.
  • a line containing the NN integers B0,,BN1B_{0}, \, \ldots, \, B_{N-1}.

Output data

The output file must contain a single line consisting of 64-bit integer ansans.

Constraints and clarifications

  • 1KN1051 \le K \le N \le 10^5.
  • 1AiN1 \le A_i \le N for each i=0N1i=0\ldots N-1.
  • 109Bi109-10^9 \le B_i \le 10^9 for each i=0N1i=0\ldots N-1.
  • IKPC stands for the International Kindergarten Programming Contest
# Score Constraints
1 0 Examples.
2 9 N20N \le 20.
3 7 N5000N \le 5000 and Bi0B_i \ge 0.
4 11 Bi0B_i \ge 0.
5 22 N5000N \le 5000.
6 51 No additional limitations.

Example 1

stdin

4 1
1 2 2 3
10 -10 -2 3

stdout

10

Explanation

In the first sample case forming the team consisting only of the first child is optimal.

Example 2

stdin

5 2
5 2 4 3 2
4 3 3 4 3

stdout

10

Explanation

In the second sample case forming one team consisting of the first child and another team consisting of the second and third children is optimal.

Log in or sign up to be able to send submissions!