Four Jewels

AtCoder
IDagc073_d
Time15000ms
Memory256MB
Difficulty
In this world, there are $K$ types of gems numbered from $1$ to $K(=4)$. The value per unit size of a gem of type $i$ is $W_i$. Snuke has $N$ gems. The $i$\-th gem is of type $A_i$ and has size $B_i$. Also, Snuke has $N$ boxes, where the $i$\-th box has size $i$. Snuke will now put one gem in each box. When a gem's size is larger than the box, the gem is cut to fit in the box. That is, when gem $i$ is put in box $j$, its value becomes $W_{A_i} \times \min(B_i, j)$. Find the maximum possible sum of gem values. ## Constraints * $K = 4$ * $1 \leq W_1 < W_2 < \cdots < W_K \leq 10^6$ * $1 \leq N \leq 250000$ * $1 \leq A_i \leq K$ * $1 \leq B_i \leq N$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $K$ $W_1$ $W_2$ $\ldots$ $W_K$ $A_1$ $B_1$ $A_2$ $B_2$ $\vdots$ $A_N$ $B_N$ [samples]
Samples
Input #1
3 4
1 2 3 4
4 2
1 3
3 2
Output #1
15

If gems $1, 2, 3$ are put in boxes $3, 1, 2$, respectively, the sum of values is $4 \times \min(2, 3) + 1 \times \min(3, 1) + 3 \times \min(2, 2) = 15$, which is maximum.
Input #2
3 4
1 2 3 4
3 1
2 2
1 3
Output #2
10
Input #3
6 4
1 3 8 10
2 2
1 4
2 2
3 1
3 4
4 3
Output #3
86
Input #4
15 4
239277 249169 419371 744281
2 14
1 4
1 11
4 12
1 7
2 12
3 15
2 5
3 4
1 8
3 2
4 1
1 15
3 5
2 8
Output #4
39858078
API Response (JSON)
{
  "problem": {
    "name": "Four Jewels",
    "description": {
      "content": "In this world, there are $K$ types of gems numbered from $1$ to $K(=4)$. The value per unit size of a gem of type $i$ is $W_i$. Snuke has $N$ gems. The $i$\\-th gem is of type $A_i$ and has size $B_i$.",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 15000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc073_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "In this world, there are $K$ types of gems numbered from $1$ to $K(=4)$. The value per unit size of a gem of type $i$ is $W_i$.\nSnuke has $N$ gems. The $i$\\-th gem is of type $A_i$ and has size $B_i$....",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments