Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. public class RoundTrip {
  7.     static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.     static StringTokenizer st;
  9.     public static void main(String[]args) throws IOException {
  10.        
  11.         int n = readInt();
  12.        
  13.         int[] cost = new int [n];
  14.         int[] gain = new int [n];
  15.         int money = 0;
  16.         int city = -1;
  17.         int counter = 0;
  18.         int adder = 0;
  19.        
  20.         for(int i = 0; i < n; i++) {
  21.             cost[i] = readInt();
  22.             gain[i] = readInt();
  23.         }
  24.     for(int k = 0; k<n;k++) {
  25.         if(city==counter) {
  26.             break;
  27.         }
  28.         for(int i = k; i <n*2; i++) {
  29.             if(i>=n) {
  30.                 adder = i - n;
  31.             }else {
  32.                 adder = i;
  33.             }
  34.             money+=gain[adder];
  35.             money-=cost[adder];
  36.             counter++;
  37.             if(money<0) {
  38.                 i = n*2; money = 0; counter = 0; break;
  39.             }
  40.             if(n == counter) {
  41.                 city = k+1; break;
  42.             }
  43.            
  44.         }
  45.     }
  46.    
  47.     System.out.println(city);
  48.        
  49.     }
  50.    
  51.     static String next() throws IOException {
  52.         while (st == null || !st.hasMoreTokens())
  53.             st = new StringTokenizer(br.readLine().trim());
  54.         return st.nextToken();
  55.     }
  56.  
  57.     static long readLong() throws IOException {
  58.         return Long.parseLong(next());
  59.     }
  60.  
  61.     static int readInt() throws IOException {
  62.         return Integer.parseInt(next());
  63.     }
  64.  
  65.     static double readDouble() throws IOException {
  66.         return Double.parseDouble(next());
  67.     }
  68.  
  69.     static char readCharacter() throws IOException {
  70.         return next().charAt(0);
  71.     }
  72.  
  73.     static String readLine() throws IOException {
  74.         return br.readLine().trim();
  75.     }
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement