272C.md (2721B)
1 Source: https://codeforces.com/problemset/problem/272/C 2 3 C. Dima and Staircase 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Dima's got a staircase that consists of *n* stairs. The first stair is at height *a*1, the second one is at *a*2, the last one is at *a**n* (1 ≤ *a*1 ≤ *a*2 ≤ ... ≤ *a**n*). 22 23 Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The *i*-th box has width *w**i* and height *h**i*. Dima throws each box vertically down on the first *w**i* stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., *w**i*. Each thrown box flies vertically down until at least one of the two following events happen: 24 25 * the bottom of the box touches the top of a stair; 26 * the bottom of the box touches the top of a box, thrown earlier. 27 28 We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width *w**i* cannot touch the stair number *w**i* + 1. 29 30 You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands. 31 32 Input 33 34 The first line contains integer *n* (1 ≤ *n* ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of *n* integers, *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 109; *a**i* ≤ *a**i* + 1). 35 36 The next line contains integer *m* (1 ≤ *m* ≤ 105) — the number of boxes. Each of the following *m* lines contains a pair of integers *w**i*, *h**i* (1 ≤ *w**i* ≤ *n*; 1 ≤ *h**i* ≤ 109) — the size of the *i*-th thrown box. 37 38 The numbers in the lines are separated by spaces. 39 40 Output 41 42 Print *m* integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input. 43 44 Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. 45 46 Examples 47 48 Input 49 50 ``` 51 5 52 1 2 3 6 6 53 4 54 1 1 55 3 1 56 1 1 57 4 3 58 ``` 59 60 Output 61 62 ``` 63 1 64 3 65 4 66 6 67 ``` 68 69 Input 70 71 ``` 72 3 73 1 2 3 74 2 75 1 1 76 3 1 77 ``` 78 79 Output 80 81 ``` 82 1 83 3 84 ``` 85 86 Input 87 88 ``` 89 1 90 1 91 5 92 1 2 93 1 10 94 1 10 95 1 10 96 1 10 97 ``` 98 99 Output 100 101 ``` 102 1 103 3 104 13 105 23 106 33 107 ``` 108 109 Note 110 111 The first sample are shown on the picture. 112 113 