#P15772. [JAG 2025 Summer Camp #2] Driving Playlist

[JAG 2025 Summer Camp #2] Driving Playlist

题目描述

You are going to go driving with mm friends. To enjoy the driving, you have a playlist of nn songs numbered 11 through nn.

At time 00 you choose one of the songs and start it from its beginning. Then, the playlist repeats forever. For each kk (1kn1 \leq k \leq n), once the song kk starts, it lasts lkl_k units of of time, and then the song k+1k+1 (or the song 11 if k=nk=n) follows immediately.

The ii-th friend, who loves the song fif_i, joins you at time ti0.5t_i - 0.5. After that, they get excited whenever the song fif_i starts. Note that even if the song fif_i is already being played when they join you, they don't get excited because everyone wants to enjoy their favorite song from the beginning.

If you choose the first song to play optimally, when is the earliest time that all the mm friends get excited at least once? Note that you cannot start playing a song from the middle.

输入格式

The input consists of a single test case of the following format.

$$\begin{aligned} & n \ m \\ & l_1 \ l_2 \ \cdots \ l_n \\ & t_1 \ t_2 \ \cdots \ t_m \\ & f_1 \ f_2 \ \cdots \ f_m \end{aligned} $$

The first line contains two integers nn and mm (1n2000001 \leq n \leq 200\,000, 1m2000001 \leq m \leq 200\,000). nn is the number of songs in the playlist and mm is the number of friends who join your driving.

The second line contains nn positive integers l1,l2,,lnl_1, l_2, \ldots, l_n. Each lil_i represents the length of the song ii. Their sum does not exceed 101510^{15}.

The third line contains mm integers t1,t2,,tmt_1, t_2, \ldots, t_m (1ti10151 \leq t_i \leq 10^{15}). Each tit_i represents that the ii-th friend joins you at time ti0.5t_i - 0.5.

The fourth line contains mm integers f1,f2,,fmf_1, f_2, \ldots, f_m (1fin1 \leq f_i \leq n). Each fif_i is the number of the favorite song of the ii-th friend.

输出格式

Output an integer, which is the earliest time that all the mm friends get excited at least once.

3 4
3 1 4
10 7 3 7
1 3 2 1
12
5 7
20 25 9 14 20
25 9 14 75 38 100 38
3 1 1 5 2 4 4
136

提示

In the first sample, if you start the playlist from song 33, the four friends first get excited at times 1212, 88, 77, and 1212, respectively.