This page is still under construction.

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

Train Travel

Interview

Time limit1sMemory limit256 MB

Summary
Count how many times each rail is crossed and pay the cheaper of single tickets or a discount card plus cheap fares for every rail.
Level

Medium4 of 10

Topics
Prefix sum, Greedy
Solved
No attempts yet

Problem

There are NN cities on a line numbered 11 to NN. Rail ii connects cities ii and i+1i+1 bidirectionally.

For each rail you may pay ticket fare AiA_i per ride, or buy a rail-specific IC card for CiC_i once and pay BiB_i per ride on that rail (Ai>BiA_i > B_i). You start with no cards.

You visit cities P1,P2,…,PMP_1, P_2, \ldots, P_M in order, moving from PjP_j to Pj+1P_{j+1} on day jj. Minimize total spending on card purchases plus rides.

Input

Line 1: NN, MM. Line 2: P1,…,PMP_1, \ldots, P_M. Next N−1N-1 lines: AiA_i, BiB_i, CiC_i for rail ii.

Output

Print the minimum total cost.

Constraints

2≤N,M≤1000002 \leq N, M \leq 100000, 1≤Bi<Ai≤1000001 \leq B_i < A_i \leq 100000, 1≤Ci≤1000001 \leq C_i \leq 100000, 1≤Pj≤N1 \leq P_j \leq N, and Pj≠Pj+1P_j \neq P_{j+1}.

Examples4

  1. Example 1

    Input
    4 4
    1 3 2 4
    120 90 100
    110 50 80
    250 70 130
    
    Expected output
    550
    
  2. Example 2

    Input
    8 5
    7 5 3 5 4
    12 5 8
    16 2 1
    3 1 5
    17 12 17
    19 7 5
    12 2 19
    4 1 3
    
    Expected output
    81
    
  3. Example 3

    Input
    3 2
    1 3
    10 5 100
    10 5 1
    
    Expected output
    16
    
  4. Example 4

    Input
    3 3
    1 2 3
    10 5 100
    10 5 1
    
    Expected output
    16