FIICode 2025 Qualifying Round - Studenți | Golderberg

This was the problem page during the contest. Access the current page here.
Time limit: 1s Memory limit: 256MB Input: Output:

Task

Joe Golderberg is a very quiet guy from Galați. He spends most of his time reading books and working as a bookseller. He also enjoys restoring books and has a special room in the basement of the bookstore for this craft. He visits this room on different days, and when he does, he can choose to put one or more books in for restoration, take one or more books out, or do nothing.

Joe received kk books from his friend, Loaf. Each book ii (1jk1 \le j \le k) has an optimal restoration time tjt_j, which represents the ideal number of days it should stay in the special room.

Additionally, each book has a maximum selling price cjc_j, which is obtained if the book spends exactly tjt_j days in the restoration room.

The selling price of a book is calculated using the formula: max(0,cjb(a+tj))max(0, c_j - |b - (a + t_j)|), where:

  • aa is the day the book was placed in restoration,
  • bb is the day the book was taken out.

Note: Joe must put all books up for restoration. Additionally, he can enter the restoration room multiple times on the same day, meaning he can leave a book in on ziz_i and pick it up on the same day ziz_i, but in this case, it will count as 00 full days of restoration.

Joe wants to buy a bouquet of flowers for Loaf and needs to maximize his profit from selling the kk books.

Input data

The first line of input contains a single integer nn (1n31051 \le n \le 3 \cdot 10^5) - the number of days in which Joe visits the restoration room.
The second line of input contains nn integers z1,z2,,znz_1,z_2,\ldots,z_n (1z1<z2<<zn1091 \le z_1 < z_2 < \ldots < z_n \le 10^9) - the days in which Joe visits the restoration room.
The third line of input contains a single integer kk (1k31051 \le k \le 3 \cdot 10^5; nk3105n \cdot k \le 3 \cdot 10^5) - the number of books.
The fourth line of input contains kk integers t1,t2,,tkt_1,t_2,\ldots,t_k (1ti1091 \le t_i \le 10^9) - the optimal times that the books need to spend in the restoration room.
The fourth line of input contains kk integers c1,c2,,ckc_1,c_2,\ldots,c_k (1ci1091 \le c_i \le 10^9) - the maximum selling prices of the books after restoration.

It is guaranteed that nk3105n \cdot k \le 3 \cdot 10^5.

Output data

Print one integer, the maximum profit you can achieve after restoring all kk books.

Example

stdin

2  
5 14  
3  
10 7 1  
3 13 14 

stdout

26

Explanation

We have n=2n = 2 available days: 55 and 1414.
We have k=3k = 3 books, each with their respective restoration times and optimal costs.

Book 11: If placed on day 55, it should ideally be taken out on day 1515, but there is no day 1515 available. The closest is 1414, meaning a penalty of 14(5+10)=1|14 - (5 + 10)| = 1. So its selling price is max(0,31)=2max(0, 3 - 1) = 2.

Book 22: If placed on day 55, it should ideally be taken out on day 1212, but we only have day 1414, meaning a penalty of 14(5+7)=2|14 - (5 + 7)| = 2. Selling price =max(0,132)=11= max(0, 13 - 2) = 11.

Book 33: If placed on day 55 and removed the same day, it gets 00 full days of restoration. Since t3=1t_3 = 1, the penalty is 5(5+1)=1|5 - (5 + 1)| = 1, so its selling price =max(0,141)=13= max(0, 14 - 1) = 13.

Total Profit =2+11+13=26= 2 + 11 + 13 = 26.

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