Problem: 2062C - Cirno and Operations
Language: java

Solution 1 (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();
    }
}

Solution 2 (runtime: 249 ms):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;

public class a_6{
    static int mod = (int)(1e9 + 7);
    static boolean MULTI_CASE = true;
    static boolean print = false;
    static int limit;
    static List<Integer> numbers;

    public static void solve() throws IOException {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for(int i = 0;i<n;i++){
            arr[i] = sc.nextInt();

        }
        int now = n-1;
        long ans = Long.MIN_VALUE;
        for(int a = 0;a<n;a++){
            long sum = 0;
            for(int i = 0;i<=now;i++){
                sum=sum+arr[i];
            }
            if(a == 0){
                ans = Math.max(ans,sum);
            }
            else{
                ans = Math.max(ans, Math.max(sum, -sum));
            }
            for(int i = 0;i<now;i++){
                arr[i] = arr[i+1]-arr[i];
            }
            now--;
        }
        System.out.println(ans);
    }
    public static Set<String> generatePermutations(String s) {
        Set<String> permutations = new TreeSet<>();
        permute(s.toCharArray(), 0, permutations);
        return permutations;
    }

    private static void permute(char[] chars, int index, Set<String> result) {
        if (index == chars.length) {
            result.add(new String(chars));
            return;
        }
        for (int i = index; i < chars.length; i++) {
            swap(chars, i, index);
            permute(chars, index + 1, result);
            swap(chars, i, index);  // backtrack
        }
    }

    private static void swap(char[] chars, int i, int j) {
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    }

    private static void reverse(long[] arr) {
        int left = 0, right = arr.length - 1;
        while (left < right) {
            long temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
    }

    public static long naturalSum(long n) {
        return 1L * (n) * (n + 1L) / 2L;
    }

    public static long firstOddSum(long n) {
        return 1L * (n) * (n);
    }

    public static long firstEvenSum(long n) {
        return 1L * (n) * (n + 1L);
    }

    private static long arithmeticSum(long start, long length) {
        return length * (2 * start + (length - 1)) / 2;
    }

    public static boolean kSmallest(long val, int k, int[] res) {
        int cnt = 0;
        for (int i : res) {
            if (i <= val)
                cnt++;
        }
        return val >= k + cnt - 1;
    }

    public static void decrementCountMap(Map<Long, Integer> map, long key) {
        if (map.containsKey(key)) {
            int count = map.get(key);
            if (count == 1) {
                map.remove(key);
            } else {
                map.put(key, count - 1);
            }
        }
    }

    public static void updateCountMap(Map<Long, Integer> map, long key) {
        int count = 0;
        if (map.containsKey(key)) {
            count = map.get(key);
        }
        map.put(key, count + 1);
    }

    private static long median(long arr[]) {
        PriorityQueue<Long> pq = new PriorityQueue<>();
        int n = arr.length;
        n = (n + 1) / 2;
        for (long i : arr) {
            pq.add(i);
            if (pq.size() > n) pq.remove();
        }
        return pq.size() > 0 ? pq.peek() : 0;
    }

    public static String DecimalToBase(long number, int base) {
        StringBuilder conv = new StringBuilder();
        while (number > 0) {
            conv.append(number % base);
            number /= base;
        }
        return conv.reverse().toString();
    }

    public static long BaseToDecimal(String base, int dig) {
        long result = 0;
        for (char digit : base.toCharArray()) {
            result *= 10;
            int baseDigit = digit - '0';
            result += (baseDigit >= dig) ? (baseDigit + 1) : baseDigit;
        }
        return result;
    }

    public static boolean isNonDecreasing(long[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < arr[i - 1]) {
                return false;
            }
        }
        return true;
    }

    static long ncr(int n, int r) {
        long sum = 1L;
        for (int i = 1; i <= r; i++) {
            sum = sum * (n - r + i) / i;
        }
        return sum;
    }

    static long fact(int n) {
        return (n == 1 || n == 0) ? 1L : 1L * n * fact(n - 1);
    }

    static int mod_mul(int x, int y) {
        return (int)((long)((x % mod) * (y % mod)) % mod);
    }

    static int mod_add(int x, int y) {
        return (int)((long)((x % mod) + (y % mod)) % mod);
    }

    static long modinv(long x, long mod) {
        return fast_pow(x, mod - 2);
    }

    static long fast_pow(long a, long b) {
        if (b == 0)
            return 1L;

        long val = fast_pow(a, b / 2);

        if (b % 2 == 0)
            return val * val % mod;
        else
            return val * val % mod * a % mod;
    }

    static boolean[] sieveOfEratosthenes(int n) {
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;

        for (int p = 2; p * p <= n; p++) {
            if (prime[p] == true) {
                for (int i = p * p; i <= n; i += p)
                    prime[i] = false;
            }
        }
        return prime;
    }

    static List<Integer> generatePrimes(int n) {
        boolean sieve[] = sieveOfEratosthenes(n);
        List<Integer> primes = new ArrayList<>();
        for (int i = 2; i <= n; i++) {
            if (sieve[i]) primes.add(i);
        }
        return primes;
    }

    static int[] singlePrimeFactor(int n) {
        int prime[] = new int[n + 1];
        for (int i = 0; i <= n; i++) prime[i] = i;

        for (int i = 2; i * i <= n; i++) {
            if (prime[i] == i) {
                for (int j = i * i; j <= n; j += i) {
                    prime[j] = i;
                }
            }
        }

        return prime;
    }

    static List<Integer> primeFactor(int n) {
        List<Integer> fact = new ArrayList<>();
        List<Integer> prime = generatePrimes(n);

        for (int i = 0; prime.get(i) * prime.get(i) <= n && i < prime.size(); i++) {
            if (n % prime.get(i) == 0) {
                while (n % prime.get(i) == 0) {
                    n /= prime.get(i);
                    fact.add(prime.get(i));
                }
            }
        }

        if (n > 1) {
            fact.add(n);
        }

        Collections.sort(prime);

        return prime;
    }

    static long lcm_of_array(List<Long> arr) {
        long lcm = arr.get(0);
        for (int i = 1; i < arr.size(); i++) {
            long num1 = lcm;
            long num2 = arr.get(i);
            long gcd_val = gcdl(num1, num2);
            lcm = (lcm * arr.get(i)) / gcd_val;
        }
        return lcm;
    }

    static int gcdi(int a, int b) {
        if (b == 0) return a;
        return gcdi(b, a % b);
    }

    static long gcdl(long a, long b) {
        if (b == 0) return a;
        return gcdl(b, a % b);
    }

    static boolean isPrime(int n) {
        if (n <= 1)
            return false;

        if (n == 2 || n == 3)
            return true;

        if (n % 2 == 0 || n % 3 == 0)
            return false;

        for (int i = 5; i < Math.sqrt(n); i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;

        return true;
    }

    public static void main(String[] args) throws IOException {
        if (MULTI_CASE) {
            int T = sc.nextInt();
            for (int i = 0; i < T; ++i) {
                solve();
            }
        } else {
            solve();
        }
        out.close();
    }

    static InputReader sc = new InputReader();
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    static class InputReader {
        private StringTokenizer st;
        private BufferedReader bf;

        public InputReader() {
            bf = new BufferedReader(new InputStreamReader(System.in));
            st = null;
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        public String nextLine() throws IOException {
            return bf.readLine();
        }

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

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

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

class Pair implements Comparable<Pair> {
    int first;
    int second;
    int third;

    Pair(int a, int b) {
        first = a;
        second = b;
    }

    Pair(int a, int b, int c) {
        first = a;
        second = b;
        third = c;
    }

    @Override
    public int compareTo(Pair other) {
        if (other.first == this.first) {
            return Integer.compare(this.second, other.second);
        }
        return Integer.compare(this.first, other.first);
    }
}

class Node {
    long val;
    List<Node> child;

    Node(long a) {
        val = a;
        child = new ArrayList<>();
    }
}