404693: GYM101611 D Decoding of Varints

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

D. Decoding of Varintstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Varint is a type used to serializing integers using one or more bytes. The key idea is to have smaller values being encoded with a smaller number of bytes.

First, we would like to encode some unsigned integer x. Consider its binary representation x = a0a1a2... ak - 1, where ai-th stands for the i-th significant bit, i.e. x = a0·20 + a1·21 + ... + ak - 1·2k - 1, while k - 1 stands for the index of the most significant bit set to 1 or k = 1 if x = 0.

To encode x we will use bytes b0, b1, ..., bm - 1. That means one byte for integers from 0 to 127, two bytes for integers from 128 to 214 - 1 = 16383 and so on, up to ten bytes for 264 - 1. For bytes b0, b1, ..., bm - 2 the most significant bit is set to 1, while for byte bm - 1 it is set to 0. Then, for each i from 0 to k - 1, i mod 7 bit of byte is set to ai. Thus,

x = (b0 - 128)·20 + (b1 - 128)·27 + (b2 - 128)·214 + ... + (bm - 2 - 128)·27·(m - 2) + bm - 1·27·(m - 1)

In the formula above we subtract 128 from b0, b1, ..., bm - 2 because their most significant bit was set to 1.

For example, integer 7 will be represented as a single byte b0 = 7, while integer 260 is represented as two bytes b0 = 132 and b1 = 2.

To represent signed integers we introduce ZigZag encoding. As we want integers of small magnitude to have short representation we map signed integers to unsigned integers as follows. Integer 0 is mapped to 0,  - 1 to 1, 1 to 2,  - 2 to 3, 2 to 4,  - 3 to 5, 3 to 6 and so on, hence the name of the encoding. Formally, if x ≥ 0, it is mapped to 2x, while if x < 0, it is mapped to  - 2x - 1.

For example, integer 75 is mapped to 150 and is encoded as b0 = 150, b1 = 1, while  - 75 will be mapped to 149 and will be encoded as b0 = 149, b1 = 1. In this problem we only consider such encoding for integers from  - 263 to 263 - 1 inclusive.

You are given a sequence of bytes that corresponds to a sequence of signed integers encoded as varints. Your goal is to decode and print the original sequence.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 10 000) — the length of the encoded sequence. The next line contains n integers from 0 to 255. You may assume that the input is correct, i.e. there exists a sequence of integers from  - 263 to 263 - 1 that is encoded as a sequence of bytes given in the input.

Output

Print the decoded sequence of integers.

ExampleInput
5
0 194 31 195 31
Output
0
2017
-2018

加入题单

上一题 下一题 算法标签: