Ash has been catching pokemons his entire life. Now he wants a different kick. He heard that BPay is offering rewards on collecting stickers!!

image

image

A person has limited stickers like Diya ,Rangoli etc. Ash has got all the stickers except Rangoli. Now he has a lot of friends who are also collecting stickers, some of whom have Rangoli.

Pikachu knows that there are N friends in total and M pairs of friends who can share stickers. To share a sticker between a pair of friends BPay charges some Poké(Pokemon Dollars).

On receiving the Rangoli sticker Ash will get a large reward R but he has to pay the total cost of sharing Rangoli for all the transactions till it reaches him.

There is a good news!! BPay will offer a 100% cashback of cost of sharing sticker between one pair of friends. You can choose which pair of friends can transfer the Rangoli and receive 100% cashback. This cost will not be included in total cost of sharing.

Can you help Ash And Pikachu find the maximum reward they can collect??

If they cannot get a positive reward or if there is no possible way , print -1.

See the sample test for clarity.

Input Format

First line contains R, reward that BPay is offering.

Second line contains N, total number of friends. Friends are numbered from 1 to N. Ash is always friend number 1.

Third line contains M, number of pairs of friends who can share stickers.

Next line contains an array of N integers stating if a friend has Rangoli sticker or not.

0≤arr[i]≤1.

arr[i] = 0, denotes that the friend does not have Rangoli.

arr[i] = 1, denotes that the friend has Rangoli.

arr[1] is always 0 as this is Ash who needs Rangoli.

Following M lines contain three integers (u, v, c) :- friend1 , friend2 and cost of sharing a sticker between them respectively.

Constraints

  • 1R109
  • 2N105
  • N-1M105
  • 0arr[i]1
  • 1c109
  • It is possible to reach from any friend to another
  • It is guaranteed that no friend is friend to himself
  • It is guaranteed that a particular pair of friends do not appear twice or more in the input

Output Format

Print the maximum reward Ash can collect after paying the total cost of sharing stickers.

Print -1 if he cannot get a positive reward or if there is no possible way to trade Rangoli.

Sample Input 0

1000
6
6
0 0 0 1 1 0
1 2 500
1 3 2
3 4 5
3 2 12
2 5 1
5 6 7

Sample Output 0

999

Explanation 0

  • Ash first makes Friend 2 and Friend 5 trade the Rangoli sticker at cost of 1.
  • Next he exchanges from Friend 2 and Friend 1(himself), at cost of 500 but applies the 100% cashback.
  • So in total he pays cost 1 and gains 1000-1 = 999 Pokedollars
Line: 1 Col: 1
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score