Question: B. Caisa and pylonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
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 0has 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 number 0 by 4 units. Then he can safely pass to the last pylon.
Question Analysis:
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. In order 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 minimum cost. Just simulate it. I feel like this question is a question of question.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define REP(i, s, t) for(int i=s;i<=t;++i)#define MAXN 100000typedef long long LL;int h[MAXN + 5];int main(){ int n; LL life = 0; LL pay = 0; cin >> n; REP(i, 1, n) cin >> h[i]; REP(i, 0, n - 1) { int val = h[i] - h[i + 1]; if (val < 0) { if (life + val < 0) { pay += -(life + val); life = 0; } else life += val; } if (val > 0) life += val; } cout << pay << endl; return 0;}
Codeforces round #264 (Div. 2) B