A. tram

Source: Internet
Author: User
Time limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Linear kingdom has exactly one tram line. It hasNStops, numbered from 1NIn
The order of tram's movement. AtI-Th stopAIPassengers
Exit the tram, whileBIPassengers
Enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.

Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any
Entering passenger enters the tram.

Input

The first line contains a single numberN(2 cores ≤ CoresNLimit ≤00001000)
-The number of the tram's stops.

ThenNLines follow, each contains two integersAIAndBI(0 bytes ≤ bytesAI, Bytes,BILimit ≤00001000)
-The number of passengers that exits the tram atI-Th stop, and the number of passengers that enter the tram atI-Th
Stop. The stops are given from the first to the last stop in the order of tram's movement.

  • The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally ,.
    This maid means thatA1 bytes = bytes 0.
  • At the last stop, all the passengers exit the tram and it becomes empty. More formally ,.
  • No passenger will enter the train at the last stop. That is,BNBytes = bytes 0.

Output

Print a single integer denoting the minimum possible capacity of the tram (0 is allowed ).

Sample test (s) Input
40 32 54 24 0
Output
6
Note

For the first example, a capacity of 6 is sufficient:

  • At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.
  • At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.
  • At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.
  • Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints.

Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. furthermore it is not possible for the tram to have a capacity less than 6. hence, 6 is the correct answer.

Solution Description: record the number of people on the bus each time they get on the bus and find the maximum value.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int main(){int number,t;int a[1002],b[1002];int max,left;scanf("%d",&number);max=0;scanf("%d %d",&a[0],&b[0]);max=b[0];left=b[0];for(t=1;t<number;t++){scanf("%d %d",&a[t],&b[t]);if(b[t]-a[t]+left>max){max=b[t]-a[t]+left;left=b[t]-a[t]+left;}else{left=b[t]-a[t]+left;}}printf("%d\n",max);return 0;}
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.