#P3582. [POI 2015 R1] 影迷 Movie-goer

[POI 2015 R1] 影迷 Movie-goer

Description

There are mm movies, numbered 1,2,,m1, 2, \ldots, m. The ii-th movie has value wiw_i.

Over nn days, exactly one movie is shown per day; on day ii, movie fif_i is shown.

You may choose l,rl, r (1lrn1 \le l \le r \le n) and watch all movies shown on days l,l+1,,rl, l+1, \ldots, r.

However, if you watch the same movie more than once, you get bored and therefore cannot obtain this movie’s value.

Now you need to maximize the total value of the movies that are watched exactly once.

Input Format

The first line contains two integers n,mn, m.

The second line contains nn integers; the ii-th is fif_i.

The third line contains mm integers; the ii-th is wiw_i.

Output Format

Print one integer, the maximum possible sum of values of movies watched exactly once.

9 4
2 3 1 1 4 1 2 4 1
5 3 6 6
15

Hint

Constraints

For 100%100\% of the testdata, 1mn1061 \le m \le n \le 10^6, 1fim1 \le f_i \le m, and 1wi1061 \le w_i \le 10^6.


Original problem name: Kinoman.

Translated by ChatGPT 5