## CTU Open Contest 2018 - Escalators

View as PDF

Points: 15
Time limit: 1.2s
Memory limit: 256M

Problem type

Binary Casino is a very special skyscraper building consisting of floors connected by a tricky network of high speed escalators. The floor connections are designed in a way that if there is an escalator going from floor to floor , then there is another escalator going from floor to floor as well. Also, for any two floors and , there is exactly one way to go from floor to floor . Your manager decided to organize a promotion game to attract more customers. The game has the following rules:

• The game is played in one or more rounds.
• In each round a customer can choose a floor on which that round starts. At this moment he earns some fixed number of tokens associated with floor . Then he may move to other floors using escalators.
• If a customer decides to take an escalator from floor to floor and has tokens he has to pay tokens to enter floor , where AND is a bitwise AND operator, is a standard minus operator on numbers, and is a number of tokens associated with floor .
• A customer can decide to stop the round on any floor (including ) and keep the tokens from that round. Then he can start a new round from any floor if it is possible.
• No two rounds may have the same pair of starting and ending floors, not even in the opposite direction, i.e. when considering exchanged starting and ending floors.

Your manager is curious about the maximum number of tokens a customer can earn in the game.

#### Input Specification

The first line of input contains an integer describing the number of floors in the casino skyscraper. The second line contains integers . The -th integer describes the number of tokens that a customer earns on the -th floor. After that, lines follow. Each line contains two integers and which describe an escalator connection between floors and .

#### Output Specification

Output a single number – the maximum number of tokens a customer can earn.

#### Sample Input 1

4
1 2 2 1
0 1
1 2
2 3

#### Sample Output 1

8

#### Sample Input 2

5
7 3 5 6 7
0 1
1 2
2 3
2 4

#### Sample Output 2

48