problems / 272

Suki's Toy Parade

easy sliding-window hashing arrays 1000 ms · 256 MB

Suki's Toy Parade

Suki has dragged every toy she owns out of her basket and lined them up along the hallway: n toys in a row, numbered 1 to n from the door. Toy i is of type t_i (all pink balls share one type, all squeaky ducks another, and so on) and gives her v_i joy. Before her walk she wants to parade exactly k consecutive toys past the neighbors' cat, and a proper parade never shows the same type of toy twice.

Choose a stretch of exactly k consecutive toys l, l+1, …, l+k−1 whose types are pairwise distinct, so that its total joy v_l + v_{l+1} + … + v_{l+k−1} is as large as possible. Print that total, or -1 if every stretch of k consecutive toys contains two toys of the same type.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2·10^5).

The second line contains n integers t_1, t_2, …, t_n (1 ≤ t_i ≤ 10^9), the toy types.

The third line contains n integers v_1, v_2, …, v_n (1 ≤ v_i ≤ 10^9), the joy values.

Output

Print one integer: the largest total joy of a stretch of exactly k consecutive toys with pairwise distinct types, or -1 if there is no such stretch.

Notes

In the first example the stretches of three toys are (types) 1 2 1, 2 1 3, 1 3 2, 3 2 4 and 2 4 4. The first and last repeat a type; the others have totals 12, 19 and 13, so the answer is 19.

In the second example both stretches of three toys contain two toys of type 5, so the answer is -1.

In the third example the only stretch is the whole row, with total 3·10^9: totals can exceed 2^31, so use 64-bit integers.

Example 1

Input

7 3
1 2 1 3 2 4 4
5 1 9 2 8 3 7

Output

19

Example 2

Input

4 3
5 7 5 5
10 10 10 10

Output

-1

Example 3

Input

3 3
1000000000 1 2
1000000000 1000000000 1000000000

Output

3000000000

Hints · reveal one at a time if you're stuck

Hint 1

Checking every stretch from scratch is too slow when k is about n/2. How do two neighbouring stretches differ?

Hint 2

Slide one window of k toys: one toy enters on the right, one leaves on the left. Keep a count per type in a hash map, plus a single number telling how many types currently appear more than once; the window is a valid parade exactly when that number is zero. Remember that totals need 64 bits.

report an issue Prefer your own editor? sooks prep 272 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.