#P3582. Unique Movie Marathon

    ID: 16835 Type: Default 1000ms 256MiB

Unique Movie Marathon

Unique Movie Marathon

There are \(m\) movies, numbered from \(1\) to \(m\), and the \(i\)-th movie has a rating of \(w_i\). Over \(n\) days, one movie is screened each day; on day \(i\), movie \(f_i\) is shown.

You can choose a contiguous segment of days \([l, r]\) (with \(1 \le l \le r \le n\)) to watch the movies. However, if you watch the same movie more than once within the chosen segment, you become bored and do not obtain its rating at all. In other words, for each movie, you only gain its rating \(w_i\) if it is watched exactly once in your selected interval.

Your task is to choose an interval that maximizes the total rating, i.e. the sum of \(w_i\) for movies watched exactly once in that interval.

inputFormat

The input begins with a line containing two integers (m) and (n). The second line contains (m) integers (w_1, w_2, \ldots, w_m), where (w_i) is the rating of the (i)-th movie. The third line contains (n) integers (f_1, f_2, \ldots, f_n), where (f_i) denotes the movie shown on day (i).

outputFormat

Output a single integer, which is the maximum total rating you can obtain by watching a contiguous interval of movies, counting only the movies that are watched exactly once.

sample

3 5
10 20 30
1 2 3 2 1
60