Friend

No attempts yetTime limit1sMemory limit16 MB

Problem

People numbered 00 to n1n-1 join a social network in stages. At stage ii, a host already in the network adds person ii using one of three protocols. IAmYourFriend makes the newcomer a friend of the host only. MyFriendsAreYourFriends makes the newcomer a friend of each current friend of the host, but not the host. WeAreYourFriends makes the newcomer a friend of the host and of each current friend of the host. Friends cannot both appear in a survey sample. Each person has a confidence value. Find the maximum total confidence of a sample with no friend pair.

Input

The first line contains nn. The second line lists confidence values. The third line lists host protocol pairs for stages 11 through n1n-1, where protocol 0,1,20,1,2 match the three rules above.

Output

Print the maximum possible total confidence of a valid sample.

Constraints

  • 2n100,0002 \leq n \leq 100{,}000
  • 11 \leq confidence 10,000\leq 10{,}000