Protective masks
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk wants to visit \(n\) stores in a given order. To enter a store, he must have a protective mask.
At the entrance to the \(i\)-th store, disposable masks are sold at a price of \(a_i\) and reusable ones at a price of \(b_i\). Zenyk will buy no more than one mask at the entrance to the \(i\)-th store. If he buys a disposable one, he will use it only in this store and then dispose of it. A reusable mask can be used in all subsequent stores.
You need to tell the minimum amount of money Zenyk will spend on masks.
Input
The first line contains an integer \(n\) — the number of stores Zenyk must visit.
The second line contains \(n\) integers \(a_i\) — the prices of disposable masks in hryvnias.
The third line contains \(n\) integers \(b_i\) — the prices of reusable masks in hryvnias.
Output
In a single line, print an integer — the minimum amount of money Zenyk will spend on masks (in hryvnias).
Constraints
\(1 \le n \le 10^5\),
\(1 \le a_i, b_i \le 10^9\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 10 12 15 11 9 8 16 100 200 150 40 47 60 74 | 77 |
| Input (stdin) | Output (stdout) |
|---|---|
| 7 10 12 15 11 9 8 16 100 200 150 50 47 60 74 | 81 |
Notes
In the first example, it is profitable for Zenyk to buy disposable masks in the first three stores and a reusable one in the fourth store. Then he will have to pay \(10+12+15+40=77\) (uah).
In the second example, it is profitable to buy disposable masks in all stores.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Protective masks - Editorial
Zenyk can buy a disposable mask in every shop. In this case he pays \(\sum_{i=1}^n a_i\). This strange notation means the sum of \(a_i\) over all \(i\) from 1 to \(n\).
If he decides to buy a reusable mask in the \(j\)-th shop, he has to buy disposable masks in all shops before the \(j\)-th one. Then he spends \(b_j + \sum_{i=1}^{j-1} a_i\). We can go over \(j\), the index of the shop where it is best to buy the reusable mask. For this we move the index \(j\) from 1 to \(n\) and keep the prefix sum of \(a_i\).
The answer is the smallest of all these options.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|