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: 311 ms):

import java.util.Scanner;

public class Sol {
    static Scanner rd = new Scanner(System.in);
    public static void main(String[] args) {
        int tt = rd.nextInt();
        while (tt-- > 0) {
            new Solution().solve();
        }
    }

    static class Solution {
        void solve() {
            int n = rd.nextInt();
            long[] a = new long[n];

            for (int i = 0; i < n; i++) {
                a[i] = rd.nextInt();
            }
            int len = n;
            if (len == 1) {
                System.out.println(a[0]);
                return;
            }
            long max = getSum(a, n, false);
            while (len > 1) {
                for (int i = 0; i < len - 1; i++) {
                    a[i] = a[i + 1] - a[i];
                }
                len--;
                max = Math.max(Math.max(getSum(a, len, true), getSum(a, len, false)), max);
            }
            System.out.println(max);
        }

        long getSum(long[] a, int len, boolean neg) {
            long sum = 0;
            for (int i = 0; i < len; i++) {
                if (neg) {
                    sum += (-1) * a[i];
                } else {
                    sum += a[i];
                }
            }
            return sum;
        }
    }
}