Han Guangming District, Eastern Province, where the High School Affiliated to University of Political Science recently implemented called " wisdom bright " smart city projects. Specific to the transport sector, through the " wisdom bright " terminal, you can see all the traffic lights bright area of the state at this moment. Xiao Ming school also installed " wisdom bright " terminal, Xiao Ming would like to take advantage of the information given in this terminal, estimated time of your own home from school.
Description of the problem once after school, Xiao Ming has been planned route of their own home, and can predict through various sections of time. Meanwhile, Xiao Ming installed by the school's " wisdom bright " terminal and see the traffic lights indicating the status of all of the way through the departure time. Please help Xiaoming computing time required for the home.
The first three input line input format contains spaces separated positive integers R & lt , Y , G , represents the set of traffic lights. The three number no more than 106 . The second line contains a positive integer into n- , represents the number of road segments and the number of lights passing through a total of Bob. Wherein the display order of the red, green and yellow: red -> green -> yellow . The next n rows, each row comprising two spaces separated by an integer K , T . k = 0 indicates a period after the road, will it takes t seconds, where t is not more than 106 ; K =. 1 , 2 , . 3 , the starting time, respectively, where the state of the traffic light is red, yellow, green and the countdown is displayed on the digital display board is t , where t are not more than R & lt , Y ,g。
Output format output a number that represents the time Bob came home from school with.
Sample input
30 3 30
8
0 10
1 5
0 11
2 2
0 6
0 3
3 10
0 3
Sample Output 46
After the first sample shows the first stage Xiaoming road, when using 10 seconds. Starting the first traffic light is red light, left 5 s; when Bob arrives intersection, the traffic light has changed to green, do not wait directly. Next, after the second segment path, when using . 11 sec. Starting the second traffic light is yellow light, left two seconds; Xiaoming when reaching the intersection, the traffic light has changed to red, left . 11 sec. Subsequently through the third, fourth segment road, when used . 9 seconds. Starting the third traffic light is green light, it left 10 s; Xiaoming when reaching the intersection, the traffic light has changed to red, left two seconds. Next, after the final section of the road with a time of 3 seconds. Total 10 + 11 + 11 + 9 + 3 + 2 = 46 is seconds
public class XiaoM { public static void main(String[] args) { // 30 3 30 // 8 // 0 10 // 1 5 // 0 11 // 2 2 // 0 6 // 0 3 // 3 10 // 0 3 List<int[]> list = new ArrayList<int []>(); list.add(new int[] {0,10}); list.add(new int[] {1,5}); list.add(new int[] {0,11}); list.add(new int[] {2,2}); list.add(new int[] {0,6}); list.add(new int[] {0,3}); list.add(new int[] {3,10}); List.add ( new new int [] { 0 , . 3 }); int Total = 0 ; int [] = Light new new int [] { 30 , 30 , . 3 }; // red, green, yellow three long lamp for ( int I = 0 ; I <list.size (); I ++ ) { int [] = ARR List. GET (I); int A = ARR [ 0 ]; IF (A == 0 ) { Total + ARR = [ . 1]; } The else { // K = l, 2,3, the departure time, respectively, where the state of the traffic light is red, yellow, green, IF (A == . 1 ) { // Red A = 0 ; } the else IF (A == . 3 ) { // green A = . 1 ; } the else { // yellow A = 2 ; } int B = (Light [A] - ARR [ . 1] + Total)% 63 is ; the while (B> Light [A]) { B = B-Light [A]; // B represents the lights when long A = (A + . 1 )% . 3 ; } IF (A == 0 ) { // red Total Light + = [ 0 ] - B; } the else IF (a == 2 ) { // yellow light, a red light but also other Total Light + = [ 2 ] -b + Light [ 0 ]; } } } . The System OUT .println (" Duration Total cost: " + Total); } }