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: 281 ms):
import javax.swing.tree.TreeNode;
import java.io.*;
import java.util.*;

/**
 * @Author: _Djhhh
 * @Introduction：
 */

public class Main {
    static FastReader fr = new FastReader();
    static FastWriter fw = new FastWriter();

    public static void solve() {
        int n=fr.nextInt();
        long ans=0;
        long[] arr = new long[n];
        for(int i=0;i<n;i++){
            arr[i]= fr.nextLong();
            ans+=arr[i];
        }
        if(n==1){
            fw.println(ans);
            return;
        }
        for(int i=0;i<n-1;i++){
            long mid=0;
            for(int j=0;j<n-i-1;j++){
                arr[j]-=arr[j+1];
                mid+=arr[j];
            }
            ans=Math.max(ans,Math.max(mid,-mid));
        }
        fw.println(ans);
    }

    public static void main(String[] args) {
        int t = 1;
       t = fr.nextInt();
        while (t-- > 0) solve();
    }
}

class DSU {
    int n;
    int[] fa;
    int[] dep;
    int[] siz;
    int[] edges;
    boolean[] v;
    DSU(int n) {
        fa = new int[n];
        dep = new int[n];
        siz = new int[n];
        edges = new int[n];
        v = new boolean[n];
        for (int i = 0; i < n; i++) {
            fa[i] = i;
            dep[i] = 0;
            siz[i] = 1;
            edges[i] = 0;
            v[i]=false;
        }
    }
    int look(int x) {
        if (fa[x] == x) return x;
        else return fa[x] = look(fa[x]);
    }
    void merge(int x, int y) {
        v[x]=v[y]=true;
        int xx = look(x);
        int yy = look(y);
        if (xx == yy) return;
        edges[xx] += edges[yy] + 1;
        siz[xx] += siz[yy];
        if (dep[xx] < dep[yy]) {
            fa[xx] = yy;
        } else {
            fa[yy] = xx;
        }

        if (dep[xx] == dep[yy]) dep[xx]++;
    }
    boolean isMerge(int x, int y) {
        return look(x) == look(y);
    }
    int getSize(int x) {
        return siz[look(x)];
    }
    int getEdges(int x) {
        return edges[look(x)];
    }
}
class Pair<T1, T2> {
    private T1 first;
    private T2 second;

    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }
    public void setFirst(T1 first) {
        this.first=first;
    }

    public void setSecond(T2 second) {
        this.second=second;
    }
    public T1 getFirst() {
        return first;
    }

    public T2 getSecond() {
        return second;
    }
}
class Algorithm {
    /**
     * 查找第一个大于等于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于等于 x 的位置
     */
    public static int lowBs(int[] arr, long x, int L, int R){
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] >= x){
                res = mid;
                r = mid - 1;
            }
            else{
                l = mid + 1;
            }
        }
        return res;
    }

    /**
     * 查找第一个大于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于 x 的位置
     */
    public static int highBs(int[] arr, long x, int L, int R) {
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] > x){
                res = mid;
                r = mid -1;
            }
            else{
                l = mid +1;
            }
        }
        return res;
    }
}
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 {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    int[] nextIntArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        int[] arr = new int[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Integer.parseInt(strNums[i]);
        }
        return arr;
    }

    long[] nextLongArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        long[] arr = new long[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Long.parseLong(strNums[i]);
        }
        return arr;
    }

    char[] nextCharArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        char[] arr = new char[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = strNums[i].charAt(0);
        }
        return arr;
    }

    char[] StringtoChar() {
        String line = nextLine();
        return line.toCharArray();
    }
}
class FastWriter {
    private final PrintWriter writer;

    public FastWriter() {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }

    public void print(Object object) {
        writer.print(object);
    }

    public void println(Object object) {
        writer.println(object);
    }

    public void flush() {
        writer.flush();
    }
}