This page is still under construction.

Parts of this page are still being built. What you see may change.

Generators

Time limit2sMemory limit512 MB

Summary
Choose power plants to build at some of m given cities and cut some of the n cycle edges so every city is powered, minimizing total cost.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Minimum spanning tree, Greedy
Solved
No attempts yet

Problem

The volcanic island of Fleeland has never had a proper electric net, but finally the administration of the island have agreed to build the island's power plants and network.

On the island's coast are its nn cities. The administration has surveyed the cities and proposed mm of them as possible locations for a power plant, with the iith proposal stating that the company can build a plant in city c_ic\_i for cost a_ia\_i.

These power plants are very modern and a single plant could power the whole island, but the volcano makes building power lines across the island a dangerous affair. For 1≤i<n1 \leq i < n, the company can build power lines between cities ii and i+1i+1 for a cost of b_ib\_i, and between cities nn and 11 for a cost of b_nb\_n. A city will receive power if it contains a power plant or is connected to a city with a power plant via power lines.

What is the cheapest way to power all the cities on the island?

Input

  • One line containing two integers nn (3≤n≤1053\leq n \leq 10^5) and mm (1≤m≤n1\leq m \leq n), the number of cities and the number of possible locations for a power plant.
  • Then follow mm lines, the iith of which contains c_ic\_i (1≤c_i≤n1 \leq c\_i \leq n) and a_ia\_i (1≤a_i≤1091 \leq a\_i \leq 10^9), the iith possible location for a power plant, and the cost to build it.
  • Then follows a line containing nn integers b_ib\_i (1≤b_i≤1091 \leq b\_i \leq 10^9), the costs of building the power lines.

The values of c_1,…,nc\_{1,\ldots,n} are unique and given in strictly increasing order.

Output

Output the minimal cost of powering all cities on the island.

Examples2

  1. Example 1

    Input
    3 2
    1 100
    2 200
    150 300 150
    
    Expected output
    400
    
  2. Example 2

    Input
    3 2
    1 100
    2 200
    300 300 150
    
    Expected output
    450