Codeforce 264 B Caisa and pylons (simulation)

Source: Internet
Author: User
Zookeeper

Caisa has n steps. The height of step I is H [I]. The energy obtained from step I, including the ground to the next step is H [I]-H [I + 1] If the energy is not enough to jump to the next step, it is necessary to supplement the energy.

Direct Simulation of water questions


#include<cstdio>#include<cstring>using namespace std;const int N = 100005;int h[N];int main(){    int n, e, ans;    scanf("%d",&n);    for (int i = 1; i <= n; ++i)        scanf ("%d", &h[i]);    ans = 0;    for (int i = e = 0; i < n; ++i)    {        if (h[i] + e < h[i + 1])        {            int t = (h[i + 1] - e - h[i]);            h[i] += t;            ans += t;        }        e += (h[i] - h[i + 1]);    }    printf ("%d\n", ans);    return 0;}

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 number 0 by 4 units. Then he can safely pass to the last pylon.



Codeforce 264 B Caisa and pylons (simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.