Separating Enemies

시간 제한2초메모리 제한2048 MB

요약
일렬로 놓인 집들 사이 도로를 끊는 비용과 서로 적대하는 집 쌍이 주어질 때, 적대하는 쌍이 모두 분리되도록 도로를 끊는 최소 비용을 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 구간, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

In the town of Unity Avenue Peace Centre (UAPC), there are NN houses all on the same side of a single road. There is a single citizen living in each house. Some pairs of citizens are enemies, and are prone to fighting if they can reach each other’s house via the road.

To prevent these fights, the town council has decided to destroy portions of the road between certain houses. The goal is to ensure that no pair of enemies can reach each other, while minimizing the cost of destruction. The cost to destroy the portion of the road between the iith and (i+1)(i+1)th house is c_ic\_i dollars. Your task is to tell the town council how much this destruction will cost.

입력

The first line of input contains an integer NN (1≤N≤1051≤N≤10^5) representing the number of houses in UAPC. The second line of input contains N−1N-1 integers c_1,c_2,…,c_N−1c\_1,c\_2,\dots ,c\_{N-1} (1≤c_i≤1041≤c\_i≤10^4), where c_ic\_i is the cost to destroy the portion of the road between the iith and (i+1)(i+1)th house. The third line of input contains an integer MM (1≤M≤1051≤M≤10^5), the number of pairs of enemies. The following MM lines each contain two integers uu and vv (1≤u\<v≤N1≤u\<v≤N), indicating that the residents of houses uu and vv are enemies.

출력

Output a single integer representing the minimum total cost required to destroy portions of the road to ensure that each pair of enemies are separated.

예제3

  1. 예제 1

    입력
    5
    1 2 3 4
    1
    1 5
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5
    1 2 3 4
    2
    1 2
    2 5
    
    예상 출력
    3
    
  3. 예제 3

    입력
    5
    1 2 3 4
    2
    1 3
    2 5
    
    예상 출력
    2