#P1230. 智力大冲浪

智力大冲浪

Description

Xiaowei signed up for China Central Television (CCTV)'s “Intellectual Surfing” show. This challenge has attracted many contestants. To honor everyone’s courage, the host first awards each contestant mm yuan. Don’t celebrate too soon, because this money may not all be yours. Next, the host announces the rules:

First, the contest time is divided into nn time slots, and the host also provides many mini-games. Each mini-game must be completed before its deadline tit_i. If a game is not completed before its deadline, a penalty wiw_i will be deducted from the mm yuan award. Here wiw_i are natural numbers, and the deduction differs from game to game. Of course, every game itself is very simple: each contestant can finish any single game within one time slot, and each game must start at the beginning of a whole time slot. The host only wants to test how each contestant schedules the order of games. As a contestant, Xiaowei wants to win the championship and, of course, the most money! Note: The contest will never make contestants lose money.

Input Format

  • The first line contains mm, the initial award for each contestant.
  • The second line contains nn, the number of mini-games.
  • The third line contains nn numbers, the deadlines of games 11 through nn.
  • The fourth line contains nn numbers, the penalty amounts if games 11 through nn are not completed before their deadlines.

Output Format

Output a single line: the maximum amount of money Xiaowei can win.

10000
7
4 2 4 3 1 4 6
70 60 50 40 30 20 10

9950

Hint

Constraints: For 100%100\% of the testdata, 1n5001 \le n \le 500, 1m5×1051 \le m \le 5 \times 10^5, 1tin1 \le t_i \le n, 1wi10001 \le w_i \le 1000.

Translated by ChatGPT 5