17 small A's candy, 17 candy

Source: Internet
Author: User

17 small A's candy, 17 candy
Description

John has N candy boxes and I has A [I] Candy.

Mr. A can eat one of the boxes of candy each time. He wants to know that to add any two adjacent boxes, there will be only x or less candy, you must eat at least a few pieces of sugar.

Input/Output Format

Input Format:

 

Enter N and x in the first line.

N integers in the second row, a [I].

 

Output Format:

 

The minimum number of sweets to be eaten.

 

Input and Output sample input sample #1:
3 32 2 2
Output sample #1:
1
Input example #2:
6 11 6 1 2 0 4
Output sample #2:
11
Input example #3:
5 93 1 4 1 5
Output sample #3:
0
Description

Example 1

Eat the candy in the second box.

Example 2

The second box eats 6, the fourth box eats 2, and the sixth box eats 3.

30% of test data, 2 <= N <= 20, 0 <= a [I], x <= 100

70% of test data, 2 <= N <= a [I], x <= 10 ^ 5

100% of test data, 2 <= N <= 10 ^ 5, 0 <= a [I], x <= 10 ^ 9

Mark

 

Mmp

Start

P = p-a [I + 1];
A [I + 1] = 0;

The write is reversed, so every time p is equal to no subtraction ,,

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 #define lli long long int  8 using namespace std; 9 void read(lli &n)10 {11     char c='+';lli x=0;bool flag=0;12     while(c<'0'||c>'9')13     {c=getchar();if(c=='-')flag=1;}14     while(c>='0'&&c<='9')15     {x=x*10+(c-48);c=getchar();}16     flag==1?n=-x:n=x;17 }18 lli a[100001];19 lli n,m;20 lli ans=0;21 int main()22 {23     read(n);read(m);24     for(lli i=1;i<=n;i++)25         read(a[i]);26     for(lli i=1;i<=n;i++)27     {28         if(a[i]+a[i+1]>m)29         {30             lli p=a[i]+a[i+1];31             if(a[i+1]>=(p-m))32             {33                 a[i+1]-=(p-m);34                 ans+=p-m;35             }36             else 37             {38                 ans+=a[i+1];39                 p=p-a[i+1];40                 a[i+1]=0;41                 a[i]-=(p-m);42                 ans+=(p-m);43             }44         }45     }46     printf("%lld",ans);47     return 0;48 }

 

Related Article

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.