Caisa solved the problem with the sugar and now he is on the way back to home.
Caisa is playing a mobile game during his path. There are (N? +? 1) pylons numbered from 0NIn this game. The pylon with number 0 has zero height, the pylon with numberI(I?>? 0) has heightHI. The goal of the game is to reachN-Th pylon, And the only move the player can do is to jump from the current pylon (Let's denote its numberK) To the next one (its number will beK? +? 1). When the player have made such a move, its energy increasesHK? -?HK? +? 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time.
Initially Caisa stand at 0 pylon and has 0 energy. the game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. caisa may use that opportunity several times, but he doesn't want to spend too much money. what is the minimal amount of money he must paid to reach the goal of the game?
Input
The first line contains integerN(1? ≤?N? ≤? 105). The next line containsNIntegersH1,H2 ,?...,HN(1 ?? ≤ ??HI?? ≤ ?? 105) representing the heights of the pylons.
Output
Print a single number representing the minimum number of dollars paid by Caisa.
Sample test (s) Input
53 4 3 2 4
Output
4
Input
34 4 4
Output
4
Note
In the first sample he can pay 4 dollars and increase the height of pylon with number0 by4 units. Then he can safely pass to the last pylon.
The question: Walking through n + 1 places in sequence, every time I go from th to th I + 1, there will be the energy of Hi-Hi + 1. If it is positive, there will be blood, if it is negative, blood is deducted. To ensure that the game is played, it is necessary to ensure that the blood is positive. You can also increase the height by spending money to offset the loss of blood and seek the least cost.
Idea: simply simulate the past
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 100005;int arr[maxn];int brr[maxn];int main() {ll ans = 0;int n, a;scanf("%d", &n);int cnt = 0;for (int i = 1; i <= n; i++) {scanf("%d", &a);if (cnt < a) {ans += a - cnt;cnt = a;}}cout << ans << endl;return 0;}
Codeforces B #264. Caisa and pylons