problems / 162

Three Copies of Every Packet

medium bit-manipulation math 1000 ms · 16 MB

Three Copies of Every Packet

A weather balloon talks to its ground station over a noisy radio link, so its firmware sends every telemetry packet three times. Copies of different packets can end up interleaved in any order. During the last flight, a firmware bug made the balloon send exactly one packet only once. Every other packet was sent and received exactly three times.

The ground station's receiver is a small microcontroller. It sees the packet IDs as a stream, in the order they arrived, and has to report the ID that was received only once. It has very little RAM, so the memory limit for this problem is 16 MB.

Input

The first line contains n (1 ≤ n ≤ 5·10^5, n mod 3 = 1), the number of received packets.

Each of the next n lines contains one packet ID a_i (−9223372036854775808 ≤ a_i ≤ 9223372036854775807, so any signed 64-bit integer).

Exactly one ID occurs once in the stream. Every other ID that occurs in the stream occurs exactly three times.

Output

Print the ID that occurs exactly once, as a signed decimal integer.

Notes

In the first example, IDs 1001 and 42 each arrive three times and 7 arrives once.

In the second example, IDs 0, −1 and 3 each arrive three times, so the answer is −6. IDs can be negative, and the answer can be negative too.

Example 1

Input

7
1001
7
1001
42
42
1001
42

Output

7

Example 2

Input

10
0
-1
3
0
-1
0
-1
3
3
-6

Output

-6

Hints · reveal one at a time if you're stuck

Hint 1

Look at the IDs one binary digit at a time. If an ID arrives three times, how does it change the number of 1s at a given bit position?

Hint 2

The lonely ID is determined by the bit positions where the count of 1s is not a multiple of 3. Can you track those counts while reading the stream, without storing the IDs?

Hint 3

Keep one counter modulo 3 for each of the 64 bit positions and update it as each value is read. At the end, rebuild the ID from the positions with remainder 1, and interpret the 64 bits as a signed two's-complement number.

report an issue Prefer your own editor? sooks prep 162 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.