Problem: 2062C - Cirno and Operations
Language: java

Solution 1 (runtime: 265 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.nio.channels.Pipe.SourceChannel;
import java.rmi.StubNotFoundException;
import java.util.*;

public class cf {
    static int mod = (int) 1e9 + 7;
    static int N = (int) 2e5 + 5;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int count = 0;

    public static void main(String[] args) {

        // boolean[] isPrime=prime(1000000009);

        int tCase = sc.nextInt();

        l1: 
        while (tCase-- > 0) {

            int n=sc.nextInt();
            long sum=0;
            long[] arr=new long[n];
            List<Long> l=new ArrayList<>();
            for(int i=0; i<n; i++){
                arr[i]=sc.nextInt();
                sum+=arr[i];
                l.add(arr[i]);
            }

            if(n==1){
                System.out.println(sum);
                continue;
            }else if(n==2){
                if(l.get(0)>0 || l.get(1)>0)
                sum=Math.max(Math.abs(l.get(0))+Math.abs(l.get(1)),sum);
                else
                sum=Math.max(sum,-Math.min(l.get(0),l.get(1))+Math.max(l.get(0),l.get(1)));
                System.out.println(sum);
                continue;
            }

            // long num1=arr[n-2],num2=arr[n-1],count=0;
            
            // for(int i=n-3;i>=1;i--,count++ ){
            //     if(count%2==0){
            //         num1-=(long)arr[i]*(n-2);
            //     }else{
            //         num1+=(long)arr[i]*(n-2);
            //     }
            // }
            // num1=count%2==0?num1-arr[0]:num1+arr[0];

            // count=0;
            // for(int i=n-2;i>=2;i--,count++ ){
            //     if(count%2==0){
            //         num2-=(long)arr[i]*(n-2);
            //     }else{
            //         num2+=(long)arr[i]*(n-2);
            //     }
            // }
            // num2=count%2==0?num2-arr[1]:num2+arr[1];
            // sum=Math.max(Math.abs(num1)+Math.abs(num2),sum);
            // System.out.println(sum);


            while(l.size()!=2){
                List<Long> tempL=new ArrayList<>();
                for(int i=1; i<l.size(); i++){
                    tempL.add(l.get(i)-l.get(i-1));
                }
                l=new ArrayList<>(tempL);
                long tempSum=findSum(l,false);
                sum=Math.max(sum,tempSum);
                tempSum=findSum(l,true);
                sum=Math.max(sum,tempSum);

            }
            sum=Math.max(Math.abs(l.get(0))+Math.abs(l.get(1)),sum);
            System.out.println(sum);
            


        }
        out.flush();

    }

    public static long findSum(List<Long> l,boolean flag){
        long sum=0;
        for(Long i:l){
            if(flag){
                sum-=i;
            }else{
                sum+=i;
            }

        }
        return Math.abs(sum);
    }

    public static long longSqrt(long n){
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        return BigInteger.valueOf(n).sqrt().longValue();
    }

    public static long fact(int num) {
        if (num == 1 || num == 0)
            return 1;
        return (num  * fact(num - 1));
    }

    public static long powerMod(long base, long exp, long mod) {
        long res = 1;
        base = base % mod;
        if (base == 0)
            return 0;

        while (exp > 0) {
            if ((exp & 1) == 1) {
                res = (res * base) % mod;
            }
            exp = exp >> 1;
            base = (base * base) % mod;
        }
        return res;
    }

    // public static HashMap<String,Pair> sortMap(HashMap<String,Pair> m){
    // List<Map.Entry<String, Pair>> list = new ArrayList<>(m.entrySet());

    // list.sort((e1,e2) -> {
    // Pair p1=e1.getValue();
    // Pair p2=e2.getValue();

    // if (p1.i != p2.i) {
    // return Integer.compare(p1.i, p2.i);
    // } else {
    // return Integer.compare(p1.j, p2.j);
    // }
    // });

    // LinkedHashMap<String, Pair> sortedMap = new LinkedHashMap<>();
    // for (Map.Entry<String, Pair> entry : list) {
    // sortedMap.put(entry.getKey(), entry.getValue());
    // }
    // return sortedMap;
    // }

    public static <K> HashMap<K, Pair> sortMap(Map<K, Pair> map) {
        List<Map.Entry<K, Pair>> list = new ArrayList<>(map.entrySet());

        list.sort((e1, e2) -> {
            Pair p1 = e1.getValue();
            Pair p2 = e2.getValue();

            if (p1.i != p2.i) {
                return Integer.compare(p1.i, p2.i);
            } else {
                return Integer.compare(p1.j, p2.j);
            }
        });

        HashMap<K, Pair> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<K, Pair> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }

    public static long sumOf2(int x, int y) {
        return (long) ((long) Math.pow(2, y + 1) - Math.pow(2, x));
    }

    static class Pair {
        int i;
        int j;

        public Pair(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

    public static BigDecimal sqrt(BigDecimal value, MathContext mc) {
        BigDecimal x = new BigDecimal(Math.sqrt(value.doubleValue()), mc);
        return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0), mc));
    }

    

    public static BigDecimal distance(BigDecimal x1, BigDecimal y1, BigDecimal x2, BigDecimal y2) {
        BigDecimal deltaX = x2.subtract(x1);
        BigDecimal deltaY = y2.subtract(y1);

        BigDecimal sumOfSquares = deltaX.multiply(deltaX).add(deltaY.multiply(deltaY));
        return sqrt(sumOfSquares, MathContext.DECIMAL128);
    }

    public static int closestMultiple(int a, int b, int k) {
        int quotient = b / a;
        int multipleBelow = a * quotient;
        int multipleAbove = a * (quotient + 1);

        int distanceBelow = Math.abs(multipleBelow - b);
        int distanceAbove = Math.abs(multipleAbove - b);
        if (distanceBelow <= k && distanceAbove <= k) {
            return distanceBelow < distanceAbove ? multipleBelow : multipleAbove;
        } else if (distanceBelow <= k) {
            return multipleBelow;
        } else if (distanceAbove <= k) {
            return multipleAbove;
        } else {
            return -1;
        }
    }

    public static boolean[] prime(int limit) {
        boolean[] isPrime = new boolean[limit + 1];
        Arrays.fill(isPrime, true);
        isPrime[0] = false;
        isPrime[1] = false;

        for (int i = 2; i * i <= limit; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= limit; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return isPrime;

    }

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        if (num <= 3) {
            return true;
        }
        if (num % 2 == 0 || num % 3 == 0) {
            return false;
        }
        for (int i = 5; i * i <= num; i += 6) {
            if (num % i == 0 || num % (i + 2) == 0) {
                return false;
            }
        }
        return true;
    }

    public static int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    // public static long combination(int n, int k) {
    // if(n < k) return 0;
    // return (fact[n] * p((fact[n - k] * fact[k]) % mod, mod - 2)) % mod;
    // }
    public static long p(long a, long b) {
        long r = 1;
        while (b > 0) {
            long res = b & 1;
            if (res == 1)
                r = (r * a) % mod;
            b /= 2;
            a = (a * a) % mod;
        }
        return r;
    }

    public static long combination(int n, int k) {
        if (k > n) {
            return 0;
        }
        if (k == 0 || k == n) {
            return 1;
        }
        k = Math.min(k, n - k);
        long c = 1;
        for (int i = 0; i < k; i++) {
            c = c * (n - i) / (i + 1);
        }
        return c;
    }

    public static int findGCD(int[] arr) {
        int result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            result = gcd(result, arr[i]);
            if (result == 1) {
                return 1;
            }
        }
        return result;
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(
                    new InputStreamReader(System.in));
        }

        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 {
                if (st.hasMoreTokens()) {
                    str = st.nextToken("\n");
                } else {
                    str = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

}


Solution 2 (runtime: 155 ms):
import java.io.*;
import java.util.*;

public class Code {

    // Input and Output
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    // For parsing integers, longs, and strings efficiently
    static StringTokenizer st;

    // Entry point
    public static void main(String[] args) throws IOException {
        int t; // Number of test cases
        t = nextInt(); // Uncomment this line if multiple test cases
        while (t-- > 0) {
            solve(); // Call the solve function for each test case
        }
        out.close(); // Close the output stream
    }

    // Solve function for each test case

    static void solve() throws IOException {
        int n = nextInt();
        long[] arr = new long[n];

        for(int i=0;i<n;i++){
            arr[i] = nextLong();
        }

        int now = n;
        long ans = Long.MIN_VALUE;

        for(int i=0;i<n;i++) {
            long sum = 0L;
            for (int j = 0; j < now; j++) {
                sum += arr[j];
            }

            if(i==0){
                ans = Math.max(ans, sum);
            }
            else{
                ans = Math.max(ans, Math.abs(sum));
            }

            for(int j = 0;j<now-1;j++){
                arr[j] = arr[j+1] - arr[j];
            }
            now--;
        }

        out.println(ans);
    }

    // Utility methods for faster input
    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

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

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

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

    char nextChar() {
        try {
            return (char) br.read();
        } catch (IOException e) {
            e.printStackTrace();
            return '\0'; // Return null character in case of an error
        }
    }

    static String nextLine() throws IOException {
        return br.readLine();
    }

    // Utility functions
    static int gcd(int a, int b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    static int lcm(int a, int b) {
        return a * (b / gcd(a, b));
    }

    static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n <= 3) return true;
        if (n % 2 == 0 || n % 3 == 0) return false;
        for (int i = 5; i * i <= n; i += 6) {
            if (n % i == 0 || n % (i + 2) == 0) return false;
        }
        return true;
    }

    static long power(long x, long y, long p) {
        long res = 1;
        x = x % p;
        while (y > 0) {
            if ((y & 1) == 1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }

    public static void sort(int[] arr)
    {
        //because Arrays.sort() uses quicksort which is dumb
        //Collections.sort() uses merge sort
        ArrayList<Integer> ls = new ArrayList<Integer>();
        for(int x: arr)
            ls.add(x);
        Collections.sort(ls);
        for(int i=0; i < arr.length; i++)
            arr[i] = ls.get(i);
    }
}