Language: C++

import java.util.*;

class Main{
    static int N;
    static long[] sum;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        sum = new long[N+1];
        long[] A = new long[N+1];
        for(int i=1;i<=N;i++) A[i]=sc.nextLong();
        for(int i=1;i<=N;i++) sum[i] = sum[i-1]+A[i];
        long ans = Long.MAX_VALUE;
        for(int i=2;i<=N-2;i++){
            long difLeft = halfClo(1,i);
            long P = (sum[i]+difLeft)/2;
            long Q = (sum[i]-difLeft)/2;
            long difRight = halfClo(i+1,N);
            long R = (sum[N]-sum[i]+difRight)/2;
            long S = (sum[N]-sum[i]-difRight)/2;
            long[] cur = new long[]{P,Q,R,S};
            Arrays.sort(cur);
            ans = Math.min(ans,cur[3]-cur[0]);
        }
        System.out.println(ans);
    }
    static long halfClo(int left, int right){
        long hi = sum[right]-sum[left-1];
        long lo = 0;
        while(hi-lo>1){
            long mid = (hi+lo)/2;
            if(check(left,right,mid)) hi = mid;
            else lo = mid;
        }
        return hi;
    }
    static boolean check(int left, int right, long target){
        long all = sum[right]-sum[left-1];
        int hi = right-1;
        int lo = left;
        while(hi-lo>1){
            int mid = (hi+lo)/2;
            long cur = sum[mid]-sum[left-1];
            if(2*cur>=all-target&&2*cur<=all+target) return true;
            if(2*cur<all-target) lo = mid;
            else hi = mid;
        }
        boolean b1 = 2*(sum[hi]-sum[left-1])>=all-target&&2*(sum[hi]-sum[left-1])<=all+target;
        boolean b2 = 2*(sum[lo]-sum[left-1])>=all-target&&2*(sum[lo]-sum[left-1])<=all+target;
        return b1||b2;
    }
}


Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.*;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:16: error: expected unqualified-id before '[' token
    5 |     static long[] sum;
      |                ^
a.cc:6:11: error: expected ':' before 'static'
    6 |     public static void main(String[] args){
      |           ^~~~~~~
      |           :
a.cc:6:29: error: 'String' has not been declared
    6 |     public static void main(String[] args){
      |                             ^~~~~~
a.cc:6:38: error: expected ',' or '...' before 'args'
    6 |     public static void main(String[] args){
      |                                      ^~~~
a.cc:37:12: error: 'boolean' does not name a type; did you mean 'bool'?
   37 |     static boolean check(int left, int right, long target){
      |            ^~~~~~~
      |            bool
a.cc:52:2: error: expected ';' after class definition
   52 | }
      |  ^
      |  ;
a.cc: In static member function 'static void Main::main(int*)':
a.cc:7:9: error: 'Scanner' was not declared in this scope
    7 |         Scanner sc = new Scanner(System.in);
      |         ^~~~~~~
a.cc:8:13: error: 'sc' was not declared in this scope
    8 |         N = sc.nextInt();
      |             ^~
a.cc:9:9: error: 'sum' was not declared in this scope
    9 |         sum = new long[N+1];
      |         ^~~
a.cc:10:13: error: structured binding declaration cannot have type 'long int'
   10 |         long[] A = new long[N+1];
      |             ^~
a.cc:10:13: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:10:13: error: empty structured binding declaration
a.cc:10:16: error: expected initializer before 'A'
   10 |         long[] A = new long[N+1];
      |                ^
a.cc:11:31: error: 'A' was not declared in this scope
   11 |         for(int i=1;i<=N;i++) A[i]=sc.nextLong();
      |                               ^
a.cc:12:49: error: 'A' was not declared in this scope
   12 |         for(int i=1;i<=N;i++) sum[i] = sum[i-1]+A[i];
      |                                                 ^
a.cc:13:20: error: 'Long' was not declared in this scope; did you mean 'long'?
   13 |         long ans = Long.MAX_VALUE;
      |                    ^~~~
      |                    long
a.cc:21:17: error: structured binding declaration cannot have type 'long int'
   21 |             long[] cur = new long[]{P,Q,R,S};
      |                 ^~
a.cc:21:17: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:21:17: error: empty structured binding declaration
a.cc:21:20: error: expected initializer before 'cur'
   21 |             long[] cur = new long[]{P,Q,R,S};
      |                    ^~~
a.cc:22:13: error: 'Arrays' was not declared in this scope
   22 |             Arrays.sort(cur);
      |             ^~~~~~
a.cc:22:25: error: 'cur' was not declared in this scope
   22 |             Arrays.sort(cur);
      |                         ^~~
a.cc:23:19: error: 'Math' was not declared in this scope
   23 |             ans = Math.min(ans,cur[3]-cur[0]);
      |                   ^~~~
a.cc:25:9: error: 'System' was not declared in this scope
   25 |         System.out.println(ans);
      |         ^~~~~~
a.cc: In static member function 'static long int Main::halfClo(int, int)':
a.cc:28:19: error: 'sum' was not declared in this scope
   28 |         long hi = sum[right]-sum[left-1];
      |                   ^~~
a.cc:32:16: error: 'check' was not declared in this scope
   32 |             if(check(left,right,mid)) hi = mid;
      |                ^~~~~