参考
import java. util. ArrayList ;
import java. util. Collections ;
import java. util. List ;
import java. util. Scanner ; public class Main { public static void main ( String [ ] args) { Scanner scanner = new Scanner ( System . in) ; int n = scanner. nextInt ( ) ; List < Pair > arr = new ArrayList < > ( ) ; int l, r; while ( scanner. hasNextInt ( ) ) { l = scanner. nextInt ( ) ; r = scanner. nextInt ( ) ; arr. add ( new Pair ( l, l + r - 1 ) ) ; } Collections . sort ( arr) ; int m = arr. size ( ) ; if ( ! check ( arr) ) { System . out. println ( - 1 ) ; } else { int pos = - 1 , diff = ( int ) 1e9 ; for ( int i = 0 ; i < m; i++ ) { if ( i == 0 ) { if ( arr. get ( i) . first >= n && Math . abs ( arr. get ( i) . first - n) < diff) { pos = 0 ; diff = Math . abs ( arr. get ( i) . first - n) ; } } else if ( arr. get ( i) . first - arr. get ( i - 1 ) . second > n && Math . abs ( arr. get ( i) . first - arr. get ( i - 1 ) . second - n) < diff) { pos = arr. get ( i - 1 ) . second + 1 ; diff = Math . abs ( arr. get ( i) . first - arr. get ( i - 1 ) . second - n) ; break ; } } if ( 99 - arr. get ( m - 1 ) . second >= n && Math . abs ( 99 - arr. get ( m - 1 ) . second - n) < diff) { pos = arr. get ( m - 1 ) . second + 1 ; } System . out. println ( pos) ; } } static class Pair implements Comparable < Pair > { int first, second; public Pair ( int first, int second) { this . first = first; this . second = second; } @Override public int compareTo ( Pair other) { return Integer . compare ( this . first, other. first) ; } } private static boolean check ( List < Pair > arr) { int m = arr. size ( ) ; for ( int i = 0 ; i < m; i++ ) { int x = arr. get ( i) . first; int y = arr. get ( i) . second; if ( x < 0 || y < 0 || x >= 100 || y >= 100 || x > y) { System . out. println ( x + " " + y) ; return false ; } if ( i > 0 && x <= arr. get ( i - 1 ) . second) { return false ; } } return true ; }
}
``