Pollywog
Contest: Codeforces Round 459 (Div. 1)
Difficulty rating: 2900
Tags: combinatorics, dp, matrices

As we all know, Dart is some kind of creature from Upside Down world. For simplicity, we call their kind pollywogs. Dart and x - 1 other pollywogs are playing a game. There are n stones in a row, numbered from 1 through n from left to right. At most 1 pollywog may be sitting on each stone at a time. Initially, the pollywogs are sitting on the first x stones (one pollywog on each stone).

Dart and his friends want to end up on the last x stones. At each second, the leftmost pollywog should jump to the right. A pollywog can jump at most k stones; more specifically, a pollywog can jump from stone number i to stones i + 1, i + 2, ... i + k. A pollywog can't jump on an occupied stone. Jumping a distance i takes ci amounts of energy from the pollywog.

Also, q stones are special Each time landing on a special stone p, takes wp amounts of energy (in addition to the energy for jump) from the pollywog. wp could be negative, in this case, it means the pollywog absorbs |wp| amounts of energy.

Pollywogs want to spend as little energy as possible (this value could be negative).

They're just pollywogs, so they asked for your help. Tell them the total change in their energy, in case they move optimally.

Input format:
The first line of input contains four integers, x, k, n and q (1 ≤ x ≤ k ≤ 8, k ≤ n ≤ 108, 0 ≤ q ≤ min(25, n - x)) — the number of pollywogs, the maximum length of jump, the number of stones and the number of special stones.

The next line contains k integers, c1, c2, ... ck, separated by spaces (1 ≤ ci ≤ 109) — the energetic costs of jumps.

The next q lines contain description of the special stones. Each line contains two integers p and wp (x + 1 ≤ p ≤ n, |wp| ≤ 109). All p are distinct.

Output format:
Print the minimum amount of energy they need, in the first and only line of output.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
2 3 10 2
1 2 3
5 -10
6 1000

Example output:
6

Example input:
4 7 85 3
17 5 28 4 52 46 6
59 -76
33 -69
19 2018

Example output:
135

Editorial:
What would we do if n was small? Notice that at any given time if i is the position of the leftmost pollywog and j is the position of the rightmost pollywog, then j - i < k. Thus, at any given time there's an i such that all pollywogs are on stones i, i + 1, ... i + k - 1, in other words, k consecutive stones.
x pollywogs are on k consecutive stones, thus, there are $${ \binom { k } { x } }$$ different ways to sit these pollywogs on k stones, that's about 70 at most. Denote dp[i][state] as the minimum amount of energy the pollywogs need to end up on stones i, i + 1, ... i + k - 1, and their positions are contained in state (there are $${ \binom { k } { x } }$$ states in total). We assume init is the initial state (pollywogs on the x first stones) and final is the final state (pollywogs on the x last stones).
Thus, we could easily update dp in $${ \mathcal { O } } ( k )$$ (where would the first pollywog jump?) using dynamic programming and this would work in $${\mathcal{O}}(n\times k\times {k \choose x})$$ since the answer is dp[n - k + 1][final].
But n is large, so what we could do is using matrix multiplication (similar to matrix multiplication, but when multiplying two matrices, we use minimum instead of sum and sum instead of multiplication, that means if C = A × B then C[i][j] = min(A[i][k] + B[k][j]) for all k) to update the dp, in case q = 0 to solve the problem in $${\mathcal {O}}\left({k \choose x}^{3}\times \log(n)\right)$$.
For q > 0, we combine the dynamic programming without matrix and with matrix. Note that the special stones only matter in updating the dp when there's a special stone among i, i + 1, ... i + k - 1, that means at most for k × q such i, for the rest we could use matrices for updating.
Total time complexity: $${\mathcal O}(\log(n){k \choose x}^{3}+qk^{2}{k \choose x})$$