火車
Problem Description
你的任務是計算火車的最小容量確保任何時候火車上的總人數都不會超過這個容量。注意,在每個車站,上車的乘客都是在下車乘客都下來之後纔開始上的。
Input
每組輸入數據第一行只有一個整數n (2 <= n <= 1000) ——車站的總數。
接下來是n行,每行有兩個整數Ai和Bi(0 <= Ai,Bi <= 1000)——分別代表在第i個車站下車和上車的乘客數。車站是沿照火車前進方向從起點到終點按順序給出的。
注意起點站不會有人下車,終點站不會有人上車。
Output
Example Input
1 4 0 3 2 5 4 2 4 0
Example Output
6
import java.util.*; |
02 |
import java.io.*; |
03 |
import java.text.*; |
04 |
public class Main
{ |
05 |
public static void main(String
args[]){ |
06 |
Scanner
input = new Scanner(System.in); |
07 |
int t
= input.nextInt(); |
08 |
while (t> 0 ){ |
09 |
t--; |
10 |
int n
= input.nextInt(); |
11 |
int ai,bi,abi; |
12 |
int sum
= 0 ; |
13 |
int []
sum1 = new int [ 1010 ]; |
14 |
|
15 |
for ( int i= 0 ;i<n;i++){ |
16 |
ai
= input.nextInt(); |
17 |
bi
= input.nextInt(); |
18 |
abi
= bi - ai; |
19 |
if (i== 0 ){ |
20 |
sum1[ 0 ]
= abi; |
21 |
} |
22 |
else { |
23 |
sum1[i]=sum1[i- 1 ]+abi; |
24 |
} |
25 |
|
26 |
} |
27 |
Arrays.sort(sum1); |
28 |
System.out.println(sum1[sum1.length- 1 ]); |
29 |
System.out.println(); |
30 |
} |
31 |
|
32 |
} |
33 |
} |