Language: Java

import java.util.*;
 
class Sushi{
	long v;
	long x;
	public Sushi(long a,long b){
		x = a;
		v = b;
	}
}

public class Main{	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long N = sc.nextInt();
		long C = sc.nextLong();
		long ans = 0;
		if(N==1){
			long position = sc.nextLong();
			long value = sc.nextLong();
			long distance = Math.min(position, C-position);
			if(value > distance) System.out.println(value-distance);
			else System.out.println(0);
			return;
		}
		
		public static ArrayList<Sushi> al = new ArrayList<Sushi>();
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		Collections.sort( al, new Comparator<Sushi>(){
	        public int compare(Sushi a, Sushi b){
	          if(a.x >= b.x) return 1;
	          else return -1;
	        }
	    });
		
		long[] calL = new long[N];
		cal[0] = al.get(0).v;
		for(int i=0;i<N;++i){
			cal[i] = cal[i-1] + al.get(i).v;
			cal[i] -= al.get(i).x;
		}
		cal[0] -= al.get(0).x;	
		long[] calR = new long[N];
		cal[N-1] = al.get(N-1).v;
		for(int i=N-1;i>=0;-i){
			cal[i] = cal[i+1] + al.get(i).v;
			cal[i] -= (C-al.get(i).x);
		}
		cal[N-1] -= (C-al.get(N-1).x);
		
		long[] maxL = new long[N];
		maxL[0] = Math.max(0, calL[0]);
		for(int i=0;i<N;++i) maxL[i] = Math.max(calL[i], max[i-1]);	
		ans = Math.max(ans, maxL[N-1]);
		long[] maxR = new long[N];
		maxR[N-1] = Math.max(0, calR[N-1]);
		for(int i=N-1;i>=0;--i) maxR[i] = Math.max(calR[i], max[i+1]);
		ans = Math.max(ans, maxR[0]);
		
		long now = 0;
		for(int i=0;i<N-1;++i){
			now += al.get(i).v;
			ans = Math.max(ans, maxR[i+1]+now-2*al.get(i).x);
		}
		now = 0;
		for(int i=N-1;i>0;--i){
			now += al.get(i).v;
			ans = Math.max(ans, maxL[i-1]+now-2*(C-al.get(i).x));
		}
		
		System.out.println(ans);
		return;
	}
}

Compiler output:
Main.java:27: error: illegal start of expression
		public static ArrayList<Sushi> al = new ArrayList<Sushi>();
		^
Main.java:28: error: illegal start of type
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		^
Main.java:28: error: > or ',' expected
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		               ^
Main.java:28: error: illegal start of type
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		                ^
Main.java:28: error: <identifier> expected
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		                   ^
Main.java:28: error: <identifier> expected
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		                           ^
Main.java:28: error: illegal start of type
		for(int i=0;i<N;++i) al.add( new Sushi(sc.nextLong(),sc.nextLong()) );
		                             ^
Main.java:29: error: <identifier> expected
		Collections.sort( al, new Comparator<Sushi>(){
		                ^
Main.java:29: error: <identifier> expected
		Collections.sort( al, new Comparator<Sushi>(){
		                    ^
Main.java:29: error: illegal start of type
		Collections.sort( al, new Comparator<Sushi>(){
		                      ^
Main.java:29: error: illegal start of type
		Collections.sort( al, new Comparator<Sushi>(){
		                                           ^
Main.java:30: error: illegal start of expression
	        public int compare(Sushi a, Sushi b){
	        ^
Main.java:34: error: class, interface, enum, or record expected
	    });
	     ^
Main.java:36: error: unnamed classes are a preview feature and are disabled by default.
		long[] calL = new long[N];
		^
  (use --enable-preview to enable unnamed classes)
Main.java:37: error: class, interface, enum, or record expected
		cal[0] = al.get(0).v;
		^
Main.java:38: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i){
		^
Main.java:38: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i){
		            ^
Main.java:38: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i){
		                ^
Main.java:40: error: class, interface, enum, or record expected
			cal[i] -= al.get(i).x;
			^
Main.java:41: error: class, interface, enum, or record expected
		}
		^
Main.java:44: error: class, interface, enum, or record expected
		cal[N-1] = al.get(N-1).v;
		^
Main.java:45: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;-i){
		^
Main.java:45: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;-i){
		              ^
Main.java:45: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;-i){
		                   ^
Main.java:47: error: class, interface, enum, or record expected
			cal[i] -= (C-al.get(i).x);
			^
Main.java:48: error: class, interface, enum, or record expected
		}
		^
Main.java:52: error: class, interface, enum, or record expected
		maxL[0] = Math.max(0, calL[0]);
		^
Main.java:53: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i) maxL[i] = Math.max(calL[i], max[i-1]);	
		^
Main.java:53: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i) maxL[i] = Math.max(calL[i], max[i-1]);	
		            ^
Main.java:53: error: class, interface, enum, or record expected
		for(int i=0;i<N;++i) maxL[i] = Math.max(calL[i], max[i-1]);	
		                ^
Main.java:54: error: class, interface, enum, or record expected
		ans = Math.max(ans, maxL[N-1]);
		^
Main.java:56: error: class, interface, enum, or record expected
		maxR[N-1] = Math.max(0, calR[N-1]);
		^
Main.java:57: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;--i) maxR[i] = Math.max(calR[i], max[i+1]);
		^
Main.java:57: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;--i) maxR[i] = Math.max(calR[i], max[i+1]);
		              ^
Main.java:57: error: class, interface, enum, or record expected
		for(int i=N-1;i>=0;--i) maxR[i] = Math.max(calR[i], max[i+1]);
		                   ^
Main.java:58: error: class, interface, enum, or record expected
		ans = Math.max(ans, maxR[0]);
		^
Main.java:61: error: class, interface, enum, or record expected
		for(int i=0;i<N-1;++i){
		^
Main.java:61: error: class, interface, enum, or record expected
		for(int i=0;i<N-1;++i){
		            ^
Main.java:61: error: class, interface, enum, or record expected
		for(int i=0;i<N-1;++i){
		                  ^
Main.java:63: error: class, interface, enum, or record expected
			ans = Math.max(ans, maxR[i+1]+now-2*al.get(i).x);
			^
Main.java:64: error: class, interface, enum, or record expected
		}
		^
Main.java:66: error: class, interface, enum, or record expected
		for(int i=N-1;i>0;--i){
		^
Main.java:66: error: class, interface, enum, or record expected
		for(int i=N-1;i>0;--i){
		              ^
Main.java:66: error: class, interface, enum, or record expected
		for(int i=N-1;i>0;--i){
		                  ^
Main.java:68: error: class, interface, enum, or record expected
			ans = Math.max(ans, maxL[i-1]+now-2*(C-al.get(i).x));
			^
Main.java:69: error: class, interface, enum, or record expected
		}
		^
Main.java:72: error: class, interface, enum, or record expected
		return;
		^
Main.java:73: error: class, interface, enum, or record expected
	}
	^
48 errors