Cod sursa(job #3290501)

Utilizator lucky1992Ion Ion lucky1992 Data 30 martie 2025 21:37:53
Problema Orase Scor 50
Compilator java Status done
Runda Arhiva de probleme Marime 2.73 kb
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

  public static class MyScanner implements Closeable {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner(String file) throws FileNotFoundException {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine(){
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }

    @Override
    public void close() throws IOException {
      br.close();
    }
  }

  public static class Pair {
    public int d;
    public int l;

    public Pair(int d, int l) {
      this.d = d;
      this.l = l;
    }
  }

  public static void main(String[] args) throws IOException {
    try (MyScanner scanner = new MyScanner("orase.in");
        PrintWriter pw = new PrintWriter(new FileOutputStream("orase.out"))) {
      scanner.nextInt();
      int N = scanner.nextInt();

      int[][] pairs2 = new int[N+1][2];
      //Pair[] pairs = new Pair[N+1];

      for (int i = 1; i <= N; i++) {
        pairs2[i] = new int[] {scanner.nextInt(), scanner.nextInt()};
        //pairs[i] = new Pair(scanner.nextInt(), scanner.nextInt());
      }

      Arrays.sort(pairs2, 1, N + 1, (int[] pairLeft, int[] pairRight) -> {
        if (pairLeft[0] == pairRight[0]) {
          return pairRight[1] - pairLeft[1];
        }

        return pairLeft[0] - pairRight[0];
      });

//      Arrays.sort(pairs, 1, N+1, (pairLeft, pairRight) -> {
//        if (pairLeft.d == pairRight.d) {
//          return pairRight.l - pairLeft.l;
//        }
//        return pairLeft.d - pairRight.d;
//      });

      //int maxBest = pairs[1].l - pairs[1].d;

      int maxBest = pairs2[1][1] - pairs2[1][0];

      int maxDist = Integer.MIN_VALUE;

      for (int i = 2; i <= N; i++) {
        maxDist = Math.max(maxDist, pairs2[i][1] + pairs2[i][0] + maxBest);
        //maxDist = Math.max(maxDist, pairs[i].l + pairs[i].d + maxBest);

//        if (maxBest < pairs[i].l - pairs[i].d) {
//          maxBest = pairs[i].l - pairs[i].d;
//        }

        if (maxBest < pairs2[i][1] - pairs2[i][0]) {
          maxBest = pairs2[i][1] - pairs2[i][0];
        }
      }

      pw.println(maxDist);
    }
  }
}