Codeforces Round #226 (Div. 2)--a Bear and Raspberry

Source: Internet
Author: User

Title Link: Bear and Raspberry



Bear and Raspberrytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the followingNDays. According to the bear's data, on theI-th(1?≤? ) I? ≤? n)Day, the price for one barrel of honey are going to be xi Kilos of raspberry.

Unfortunately, the bear had neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who was ready to lend him a barrel of honey for exactly one day forCkilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some dayD(1?≤? ) D. <? n), lent a barrel of honey and immediately (onD) Sell it according to a daily exchange rate. The next day(D. +?1)The bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from Selli Ng the previous barrel) and immediately (on D. +?1) give his friend, the borrowed barrel of honey as well asCKilograms of raspberry for renting the barrel.

The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note if at some point of the plan the bear runs out of the raspberry and then he won ' t execute such a plan.

Input

The first line contains the space-separated integers, n and C (2?≤? n? ≤?100,?0?≤? c. ≤?100),-the number of days and the number of kilos of raspberry, the bear should give for borrowing The barrel.

The second line containsNspace-separated integers x1,? x 2,?...,? x N (0?≤? ) xi? ≤?100), the price of a honey barrel on dayI.

Output

Print a single integer-the answer to the problem.

Sample Test (s) input
5 15 10 7 3 20
Output
3
Input
6 2100 1 10 40 10 40
Output
97
Input
3 01 2 3
Output
0
Note

In the first sample, the bear would lend a honey barrel at day 3 and then sell it for 7. Then the bear would buy a barrel for 3 and return it to the friend. So, the profit is (7-3-1) = 3.

In the second sample bear would lend a honey barrel at day 1 and then sell it for 100. Then the bear to buy the barrel for 1 on the day 2. So, the profit is (100-1-2) = 97.







Problem-solving ideas: The subject matter is to give a sequence, find the next one with the largest gap between the value, and the result to subtract C. Direct greed and violence, first sweep over, find the next element in the adjacent element and the previous element of the largest difference in ans, if ans < C, then ans = 0; otherwise ans = ans-c.






AC Code:

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h > #include <stdlib.h> #include <time.h>using namespace std; #define INF 0x7fffffffint Main () {    #ifdef Sxk        freopen ("In.txt", "R", stdin);    #endif    int N, c, a, b;    while (scanf ("%d%d", &n, &c)!=eof)    {        int ans = 0;        A = 0;        for (int i=1; i<=n; i++) {            cin>>b;            if (A-b > ans) ans = a A;            A = b;        }        if (ans > c) ans-= c;    else ans = 0;    cout<<ans<<endl;    }    return 0;}




Codeforces Round #226 (Div. 2)--a Bear and Raspberry

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.