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