|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + static int answer,N; |
| 6 | + public static void main(String[] args) throws Exception { |
| 7 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + |
| 9 | + N = Integer.parseInt(br.readLine()); |
| 10 | + |
| 11 | + List<int []> list = new ArrayList<>(); |
| 12 | + StringTokenizer st; |
| 13 | + for(int i =0; i<N; i++) { |
| 14 | + st = new StringTokenizer(br.readLine()); |
| 15 | + |
| 16 | + int S = Integer.parseInt(st.nextToken()); |
| 17 | + int W = Integer.parseInt(st.nextToken()); |
| 18 | + |
| 19 | + list.add(new int[]{S,W}); |
| 20 | + } |
| 21 | + |
| 22 | + back(0, 0, list); |
| 23 | + |
| 24 | + System.out.println(answer); |
| 25 | + } |
| 26 | + |
| 27 | + static void back(int curIndex, int value, List<int[]> list) { |
| 28 | + if(curIndex == N) { |
| 29 | + answer = Math.max(answer, value); |
| 30 | + return; |
| 31 | + } |
| 32 | + answer = Math.max(answer, value); |
| 33 | + |
| 34 | + int realCurIndex = curIndex; |
| 35 | + int realValue = value; |
| 36 | + for(int i = curIndex; i<N; i++) { |
| 37 | + if(list.get(i)[0] >=1) { |
| 38 | + realCurIndex = i; |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if(list.get(realCurIndex)[0] <=0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + for(int i =0; i<N; i++) { |
| 49 | + if(realCurIndex == i) continue; |
| 50 | + |
| 51 | + if(list.get(i)[0] >=1) { |
| 52 | + list.get(realCurIndex)[0] -= list.get(i)[1]; |
| 53 | + list.get(i)[0] -= list.get(realCurIndex)[1]; |
| 54 | + if(list.get(realCurIndex)[0] <=0 && list.get(i)[0] <=0) { |
| 55 | + back(realCurIndex+1, realValue+2, list); |
| 56 | + } |
| 57 | + else if(list.get(realCurIndex)[0] <=0) { |
| 58 | + back(realCurIndex+1, realValue+1, list); |
| 59 | + } |
| 60 | + else if (list.get(i)[0] <=0) { |
| 61 | + back(realCurIndex+1, realValue+1, list); |
| 62 | + } |
| 63 | + else { |
| 64 | + back(realCurIndex+1, realValue, list); |
| 65 | + } |
| 66 | + list.get(realCurIndex)[0] += list.get(i)[1]; |
| 67 | + list.get(i)[0] += list.get(realCurIndex)[1]; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments