#B. Coin Toss Earnings

    Type: Default 1000ms 256MiB

Coin Toss Earnings

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Coin Toss Earnings

Problem Statement

Bessie is planning to toss a coin NN (1MN5000)(1 \leq M \leq N \leq 5000) times. She also has a counter, which starts at 00. The outcome of each toss will impact the counter and Bessie's earnings in the following ways:

  • If the coin lands on heads: Bessie increases the counter by 1 and receives XiX_i (1Xi109)(1 \leq X_i \leq 10^9) dollars.
  • If the coin lands on tails: she resets the counter to 00 and doesn't receive any money.

Additionally, there are MM (MN)(M \leq N) types of streak bonuses. For each type ii, Bessie earns a bonus of YiY_i (1Yi109)(1 \leq Y_i \leq 10^9) dollars each time the counter shows CiC_i.

Bessie is interested in finding out the maximum amount of money she can potentially earn from this coin-tossing game.

INPUT FORMAT (input arrives from the terminal / stdin):

The input is provided in the following format:

  • The first line contains two integers NN and MM.
  • The second line contains NN integers, XiX_i (1AiN)(1 \leq A_i \leq N) for 1iN1 \leq i \leq N, separated by spaces.
  • The next MM lines each contain two integers CiC_i and YiY_i.

OUTPUT FORMAT (print output to the terminal / stdout):

Print a single integer: the maximum amount of money that Bessie can earn from the coin-tossing game.

SAMPLE INPUT 1:

6 3
2 7 1 8 2 8
2 10
3 1
5 5

SAMPLE OUTPUT 1:

48

If Bessie gets heads, heads, tails, heads, heads, heads, in this order, the following amounts of money are awarded:

  • In the 1st coin toss, the coin lands on heads. The counter's value changes from 00 to 11 and Bessie receives 22 dollars.
  • In the 2nd coin toss, the coin lands on heads. The counter's value changes from 11 to 22 and Bessie receives 77 dollars. Additionally, she gets 10 dollars as a streak bonus.
  • In the 3rd coin toss, the coin lands on tails. The counter's value changes from 22 to 00.
  • In the 4th coin toss, the coin lands on heads. The counter's value changes from 00 to 11 and Bessie receives 88 dollars.
  • In the 5th coin toss, the coin lands on heads. The counter's value changes from 11 to 22 and Bessie receives 22 dollars. Additionally, she gets 1010 dollars as a streak bonus.
  • In the 6th coin toss, the coin lands on heads. The counter's value changes from 22 to 33 and Bessie receives 88 dollars. Additionally, she gets 11 dollar as a streak bonus.

In this case, Bessie earns 2+(7+10)+0+8+(2+10)+(8+1)=482 + (7 + 10) + 0 + 8 + (2 + 10) + (8 + 1) = 48 dollars in total, which is the maximum possible.

Note that streak bonuses are awarded any number of times each time the counter shows CiC_i.

As a side note, if Bessie gets heads in all 66 coin tosses, she only receives 2+(7+10)+(1+1)+8+(2+5)+8=442 + (7 + 10) + (1 + 1) + 8 + (2 + 5) + 8 = 44 dollars, which is not the maximum.

SCORING:

  • For all inputs: No additional constraints.

HFI Coding Club 2023 November Contest

Not Attended
Status
Done
Rule
IOI
Problem
4
Start at
2023-11-16 18:45
End at
2023-11-16 20:45
Duration
2 hour(s)
Host
Partic.
21