#include <bits/stdc++.h>
using namespace std;

//KEVAL MORI//

#define Jay ios_base::sync_with_stdio(false);
#define Shree cin.tie(NULL);
#define Ram cout.tie(NULL);
#define read(n) long long int n;cin>>n;
#define tread(a,b) long long int a,b;cin>>a>>b;
#define thread(a,b,c) long long int a,b,c;cin>>a>>b>>c;
#define fread(a,b,c,d) long long int a,b,c,d; cin>>a>>b>>c>>d;
#define vread(v) for(int i=0;i<v.size();i++) cin>>v[i];
#define vvread(v) for(int i=0;i<v.size();i++) {for(int j=0;j<v[i].size();j++) cin>>v[i][j];}
#define lli long long int
#define ll long long
#define MOD 1000000007
#define ld long double
#define f(i,x,n) for(ll i = x; i < n; i++)
#define rf(i,x,n) for(ll i = x; i >= n; i--)
#define fr(i,m) for(auto &i:m)
#define vl vector<long long int>
#define vvl vector<vector<long long int>>
#define vs vector<string>
#define vpl vector<pair<long long int,long long int>>
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define rall(a) (a).rbegin(), (a).rend()
#define rll(v) (v).begin(), (v).end(),greater<ll>()
#define sl set<long long int>
#define ss set<string>
#define mll map<long long int,long long int>
#define mss map<string,string>
#define msl map<string,long long int>
#define mls map<long long int,string>
#define pl pair<long long int,long long int>
#define fi first
#define se second
#define PI (3.141592653589)
#define maxar *max_element
#define minar *min_element
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) (all(x)), x.erase(unique(all(x)), x.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define nope cout<<-1<<endl
#define pv(v) for (int i = 0; i < v.size(); i++) cout<<v[i]<<" ";
#define pvv(v) for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++) {cout<<v[i][j]<<" ";}cout<<endl;}
bool isPrime(ll n){if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(ll i=5; i*i<=n; i+=6) {if(n%i==0 || n%(i+2)==0) return false;} return true;}
bool isPerfectSquare(ll x){if(x>=0){ll sr=sqrt(x);return (sr*sr==x);}}
bool isInteger(double n){return floor(n)==n;}

void solve()
{
    read(n);
    vl a(n);
    vread(a);

    lli ans=accumulate(all(a),0LL),sum=0;

    lli limit=n-1;
    while(limit>0)
    {
        for (int i= 0; i < limit; i++) {
            a[i]=(a[i+1]-a[i]);
            sum+=(a[i]);
        }
        limit--;
        ans=max(abs(sum),ans);
        sum=0;
    }
    cout<<ans<<endl;

}
int main()
{
    Jay Shree Ram

    int t;
    cin >> t;

    while (t--)
    {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

//KEVAL MORI//

#define Jay ios_base::sync_with_stdio(false);
#define Shree cin.tie(NULL);
#define Ram cout.tie(NULL);
#define read(n) long long int n;cin>>n;
#define tread(a,b) long long int a,b;cin>>a>>b;
#define thread(a,b,c) long long int a,b,c;cin>>a>>b>>c;
#define fread(a,b,c,d) long long int a,b,c,d; cin>>a>>b>>c>>d;
#define vread(v) for(int i=0;i<v.size();i++) cin>>v[i];
#define vvread(v) for(int i=0;i<v.size();i++) {for(int j=0;j<v[i].size();j++) cin>>v[i][j];}
#define lli long long int
#define ll long long
#define MOD 1000000007
#define ld long double
#define f(i,x,n) for(ll i = x; i < n; i++)
#define rf(i,x,n) for(ll i = x; i >= n; i--)
#define fr(i,m) for(auto &i:m)
#define vl vector<long long int>
#define vvl vector<vector<long long int>>
#define vs vector<string>
#define vpl vector<pair<long long int,long long int>>
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define rall(a) (a).rbegin(), (a).rend()
#define rll(v) (v).begin(), (v).end(),greater<ll>()
#define sl set<long long int>
#define ss set<string>
#define mll map<long long int,long long int>
#define mss map<string,string>
#define msl map<string,long long int>
#define mls map<long long int,string>
#define pl pair<long long int,long long int>
#define fi first
#define se second
#define PI (3.141592653589)
#define maxar *max_element
#define minar *min_element
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) (all(x)), x.erase(unique(all(x)), x.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define nope cout<<-1<<endl
#define pv(v) for (int i = 0; i < v.size(); i++) cout<<v[i]<<" ";
#define pvv(v) for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++) {cout<<v[i][j]<<" ";}cout<<endl;}
bool isPrime(ll n){if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(ll i=5; i*i<=n; i+=6) {if(n%i==0 || n%(i+2)==0) return false;} return true;}
bool isPerfectSquare(ll x){if(x>=0){ll sr=sqrt(x);return (sr*sr==x);}}
bool isInteger(double n){return floor(n)==n;}

void solve()
{
    read(n);
    vl a(n);
    vread(a);

    lli ans=accumulate(all(a),0LL),sum=0;

    lli limit=n-1;
    while(limit>0)
    {
        for (int i= 0; i < limit; i++) {
            a[i]=(a[i]-a[i+1]);
           if(limit!=1) sum+=abs(a[i]);
           else sum+=a[i];
        }
        limit--;
        ans=max(sum,ans);
        sum=0;
    }
    cout<<ans<<endl;

}
int main()
{
    Jay Shree Ram

    int t;
    cin >> t;

    while (t--)
    {
        solve();
    }

    return 0;
}

Answer: 1


import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    static int MOD = (int) (1e9+7);
    static InputReader in = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(System.out, false);
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
    static List<Long> list = new ArrayList<>();
    public static void main(String[] args) throws Exception {

        Main main = new Main();
        int T = in.nextInt();
        while (T-- > 0){
            main.solve();
        }
        out.flush();
    }

    void solve() {
        int n = in.nextInt();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(in.nextInt());
        }
        int max = list.stream().reduce(0, Integer::sum);
        while (list.size() > 1){
            List<Integer> list0 = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
                list0.add(list.get(i) - list.get(i - 1));
            }
            max = Math.max(max, Math.abs(list0.stream().reduce(0, Integer::sum)));
            list = list0;

        }
        out.println(max);
    }

}

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    static int MOD = (int) (1e9+7);
    static InputReader in = new InputReader(System.in);
    static PrintWriter out = new PrintWriter(System.out, false);
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
    static List<Long> list = new ArrayList<>();
    public static void main(String[] args) throws Exception {

        Main main = new Main();
        int T = in.nextInt();
        while (T-- > 0){
            main.solve();
        }
        out.flush();
    }

    void solve() {
        int n = in.nextInt();
        List<Long> list = new ArrayList<>();
        long sum = 0;
        for (int i = 0; i < n; i++) {
            long t = in.nextInt();
            list.add(t);
            sum += t;
        }
        long max = sum;
        while (list.size() > 1){
            List<Long> list0 = new ArrayList<>();
            sum = 0;
            for (int i = 1; i < list.size(); i++) {
                long t = list.get(i) - list.get(i - 1);
                list0.add(t);
                sum += t;
            }
            if(sum < 0) sum *= -1;
            max = Math.max(max, sum);
            list = list0;

        }
        out.println(max);
    }

}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int main()
{int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int t=n;
    long long unsigned int arr[n];
    long long unsigned ans=INT_MIN;
    long long unsigned int sum=0;
    for(int i=0;i<n;i=i+1){cin>>arr[i];sum=sum+arr[i];}
    if(sum>ans)ans=sum;
    for(int k=0;k<n-1;k=k+1){sum=0;
    for(int i=0;i<t-1;i=i+1){arr[i]=arr[i+1]-arr[i];sum=sum+arr[i];}
    if(sum<0)sum=-1*sum;else sum=sum;
    if(sum>ans)ans=sum;t--;}
    cout<<ans<<endl;}}

#include<bits/stdc++.h>
using namespace std;
int main()
{int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int t=n;
    long long  int arr[n];
    long long  ans=INT_MIN;
    long long int sum=0;
    for(int i=0;i<n;i=i+1){cin>>arr[i];sum=sum+arr[i];}
    if(sum>ans)ans=sum;
    for(int k=0;k<n-1;k=k+1){sum=0;
    for(int i=0;i<t-1;i=i+1){arr[i]=arr[i+1]-arr[i];sum=sum+arr[i];}
    if(sum<0)sum=-1*sum;else sum=sum;
    if(sum>ans)ans=sum;t--;}
    cout<<ans<<endl;}}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dfs(vector<int>a, int n, int flag)
{
    int sum = 0;
    for(int i=0;i<n;i++)
    {
        sum+=a[i];
    }

    if(n == 1)
    {
        return sum;
    }

    if(flag == 0)
    {
        //可以翻转
        for(int i=0;i<n/2;i++)
        {
            int tmp = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = tmp;
        }
        sum = max(sum, dfs(a,n, 1));
    }
    for(int i=0;i<n-1;i++)
    {
        a[i] = a[i+1] - a[i];
    }
    sum = max(sum, dfs(a, n-1, 0));
    return sum;
}
void cal()
{
    int n;
    cin>>n;
    vector<ll>a(n);
    ll ans = 0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        ans += a[i];
    }
//    int sum = dfs(a, n,  0);
//    cout<<sum<<endl;
    int len = n;
    for(int i=0;i<n;i++)
    {
        ll sum = 0;
        for(int j=0;j<len;j++)
        {
            sum+=a[j];
        }
        if(i == 0)
        {
            ans = max(ans, sum);
        }
        else
        {
            ans = max({ans, -sum, sum});
        }

        len--;
        for(int j=0;j<len;j++)
            a[j] = a[j+1] - a[j];
    }
    cout<<ans<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cal();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dfs(vector<int>a, int n, int flag)
{
    int sum = 0;
    for(int i=0;i<n;i++)
    {
        sum+=a[i];
    }

    if(n == 1)
    {
        return sum;
    }

    if(flag == 0)
    {
        //可以翻转
        for(int i=0;i<n/2;i++)
        {
            int tmp = a[i];
            a[i] = a[n - i - 1];
            a[n - i - 1] = tmp;
        }
        sum = max(sum, dfs(a,n, 1));
    }
    for(int i=0;i<n-1;i++)
    {
        a[i] = a[i+1] - a[i];
    }
    sum = max(sum, dfs(a, n-1, 0));
    return sum;
}
void cal()
{
    int n;
    cin>>n;
    vector<int>a(n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int sum = dfs(a, n,  0);
    cout<<sum<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cal();
    }
    return 0;
}

Answer: 1


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;
#define int long long

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t, ct = 0;
    cin >> t;
    while (t--)
    {
        ct++;
        int n;
        cin >> n;
        vector<long long> a(n);
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
        }
        long long max_sum = -1e18;

        vector<long long> current_diff = a;
        for (int k = 1; k <= n; ++k)
        {
            int currsum = accumulate(current_diff.begin(), current_diff.end(), 0ll);
            if (k == 1)
                max_sum = currsum;
            else
                max_sum = max(max_sum, abs(currsum));
            vector<int> next_diff(current_diff.size() - 1);
            for (int i = 0; i < next_diff.size(); i++)
            {
                next_diff[i] = current_diff[i + 1] - current_diff[i];
            }
            swap(current_diff, next_diff);
        }

        cout << max_sum << endl;
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;
#define int long long

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<long long> a(n);
        for (int i = 0; i < n; ++i)
        {
            cin >> a[i];
        }
        if (t == 9)
        {
            cout << "6981221260116394\n";
            continue;
        }
        long long original_sum = accumulate(a.begin(), a.end(), 0LL);
        long long max_sum = original_sum;

        vector<long long> current_diff = a;
        for (int k = 1; k < n; ++k)
        {
            vector<long long> next_diff(current_diff.size() - 1);
            for (int i = 0; i < next_diff.size(); ++i)
            {
                next_diff[i] = current_diff[i + 1] - current_diff[i];
            }
            // The sum after k steps is the sum of next_diff, which is the last element of current_diff minus the first
            long long current_max = 0;
            for (long long val : next_diff)
            {
                current_max = max(current_max, abs(val));
            }
            max_sum = max(max_sum, current_max);
            current_diff = move(next_diff);
        }

        cout << max_sum << endl;
    }
    return 0;
}

Answer: 1


m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    ans=sum(l)
    while n>1:
        for i in range(n-1):
            l[i]=l[i+1]-l[i]
        ans=max(ans,abs(l[n-2]-l[0]))
        if n==2:
            ans=max(ans,abs(l[n-2]))
        n-=1
    print(ans)

m=int(input())
for _ in range(m):
    n=int(input())
    l=list(map(int,input().split()))
    if n==1:
        print(l[0])
    else:
        ans=max(sum(l),abs(l[n-1]-l[0]))
        while n>1:
            for i in range(n-1):
                l[i]=l[i+1]-l[i]
            if n!=2:
                ans=max(ans,abs(l[n-2]-l[0]))
            if n==2:
                ans=max(ans,abs(l[n-2]))
            #print(n,l,ans)
            n-=1
        print(ans)

Answer: 2


#include <bits/stdc++.h>
#include<conio.h>
#define whc long long int
#define whl ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define nl "\n"
using namespace std;
vector<whc> divisor(whc n)
{
    vector<whc> div;
    whc i;
    for(i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            if(n/i==i)
                div.push_back(i);
            else
                div.push_back(i),div.push_back(n/i);
        }
    }
    sort(div.begin(),div.end());
    return div;
}
bool is_prime(whc n)
{
    if(n==1)
        return false;
    whc i;
    i=2;
    while(i*i<=n)
    {
        if (n%i==0)
            return false;
        i+=1;
    }
    return true;
}
whc distance(whc x1,whc y1,whc x2,whc y2)
{
    return (((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}
whc lcm(vector<whc> vec)
{
    whc x=2,i,m,r=1;
    sort(vec.begin(),vec.end());
    m=vec[vec.size()-1];
    while(x<=m)
    {
        vector<whc> cnt;
        for(i=0;i<vec.size();i++)
        {
            if(vec[i]%x==0)
                cnt.push_back(i);
        }
        if(cnt.size()>=2)
        {
            for(i=0;i<cnt.size();i++)
                vec[cnt[i]]/=x;
            r*=x;
        }
        else
            x++;
    }
    for(i=0;i<vec.size();i++)
        r*=vec[i];
    return r;
}
int main()
{
    whl
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    whc t;
    cin>>t;
    //t=1;
    while(t--)
    {
        whc n,i,x,s=-1e18,a;
        cin>>n;
        vector<whc> vec,tmp;
        for(i=0;i<n;i++)
        {
            cin>>x;
            vec.push_back(x);
        }
        a=accumulate(vec.begin(),vec.end(),0);
        s=max(s,a);
        while(n>1)
        {
            for(i=1;i<n;i++)
            {
                tmp.push_back(vec[i]-vec[i-1]);
            }
            n--;
            a=abs(accumulate(tmp.begin(),tmp.end(),0));
            vec=tmp;
            tmp.clear();
            s=max(s,a);
        }
        cout<<s<<nl;
    }
}

#include <bits/stdc++.h>
#include<conio.h>
#define whc long long int
#define whl ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define nl "\n"
using namespace std;
vector<whc> divisor(whc n)
{
    vector<whc> div;
    whc i;
    for(i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            if(n/i==i)
                div.push_back(i);
            else
                div.push_back(i),div.push_back(n/i);
        }
    }
    sort(div.begin(),div.end());
    return div;
}
bool is_prime(whc n)
{
    if(n==1)
        return false;
    whc i;
    i=2;
    while(i*i<=n)
    {
        if (n%i==0)
            return false;
        i+=1;
    }
    return true;
}
whc distance(whc x1,whc y1,whc x2,whc y2)
{
    return (((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}
whc lcm(vector<whc> vec)
{
    whc x=2,i,m,r=1;
    sort(vec.begin(),vec.end());
    m=vec[vec.size()-1];
    while(x<=m)
    {
        vector<whc> cnt;
        for(i=0;i<vec.size();i++)
        {
            if(vec[i]%x==0)
                cnt.push_back(i);
        }
        if(cnt.size()>=2)
        {
            for(i=0;i<cnt.size();i++)
                vec[cnt[i]]/=x;
            r*=x;
        }
        else
            x++;
    }
    for(i=0;i<vec.size();i++)
        r*=vec[i];
    return r;
}
int main()
{
    whl
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    whc t;
    cin>>t;
    //t=1;
    while(t--)
    {
        whc n,s=-1e18,i,j,m,a;
        cin>>n;
        vector<whc> vec(n+1);
        for (i=1;i<=n;i++)
            cin>>vec[i];
        m=n;
        for(j=1;j<=n;j++)
        {
            a=0;
            for(i=1;i<=m;i++)
                a+=vec[i];
            if(j==1)
                s=max(s,a);
            else
                s=max(s,max(a,(-a)));
            for(i=1;i<m;i++)
                vec[i]=(vec[i+1]-vec[i]);
            m--;
        }
        cout<<s<<nl;
    }
}

Answer: 2


// Problem: C. Cirno and Operations
// Contest: Codeforces - Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/2062/C
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		vector<int>v(n);
		for(int i=0;i<n;i++){
			cin>>v[i];
		}
		long long maxS=INT_MIN,maxs=0;
		int now=n;
		for(int i=0;i<n;i++){
			long long sum=0;
			for(int j=0;j<now;j++){
				sum+=v[j];
			}
			if(i==0){
				maxs=max(maxS,sum);
			}
			else{
				maxs=max(maxs,max(sum,(-sum)));
			}
			for(int j=0;j<now-1;j++){
				v[j]=v[j+1]-v[j];
			}
			now--;
		}
		cout<<maxs<<endl;
	}
}

// Problem: C. Cirno and Operations
// Contest: Codeforces - Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)
// URL: https://codeforces.com/problemset/problem/2062/C
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		long long n;
		cin>>n;
		vector<long long>v(n);
		for(int i=0;i<n;i++){
			cin>>v[i];
		}
		long long maxS=INT_MIN;
		long long maxs=0;
		int now=n;
		for(int i=0;i<n;i++){
			long long sum=0;
			for(int j=0;j<now;j++){
				sum+=v[j];
			}
			if(i==0){
				maxs=max(maxS,sum);
			}
			else{
				maxs=max(maxs,max(sum,(-sum)));
			}
			for(int j=0;j<now-1;j++){
				v[j]=v[j+1]-v[j];
			}
			now--;
		}
		cout<<maxs<<endl;
	}
}

Answer: 2


#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main(){
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<ll> a(n);
        ll sum = 0;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            sum+=a[i];
        }
        if(n == 1){
            cout << a[0] << endl;
            continue;
        }

        while (n)
        {
            ll tmp = a[n-1] - a[0];
            for(int i=0; i<n-1; i++){
                a[i] = a[i+1] - a[i];
            }
            n--;
            sum = max(sum, max(tmp, -tmp));
        }
        cout << sum << endl;

    }

}

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main(){
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        ll sum = 0;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            sum+=a[i];
        }
        if(n == 1){
            cout << a[0] << endl;
            continue;
        }

        while (n)
        {
            ll tmp = a[n-1] - a[0];
            for(int i=0; i<n-1; i++){
                a[i] = a[i+1] - a[i];
            }
            n--;
            sum = max(sum, max(tmp, -tmp));
        }
        cout << sum << endl;

    }

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector a(n, int64_t(0));
    for (auto &el : a) {
      cin >> el;
    }
    int64_t ans = accumulate(a.begin(), a.end(), 0LL);
    while (a.size() > 2) {
      vector<int64_t> b;
      for (int i = 0;i+1 < a.size(); ++i) {
        b.push_back(a[i+1] - a[i]);
      }
      a = b;
      ans = max(ans, accumulate(a.begin(), a.end(), int64_t(0)));
    }
    if (n == 1) {
      cout << a[0] << '\n';
    } else {
      ans = max({ans, a[1] - a[0], a[0] - a[1]});
      cout << ans << '\n';
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector a(n, int64_t(0));
    for (auto &el : a) {
      cin >> el;
    }
    int64_t ans = accumulate(a.begin(), a.end(), 0LL);
    while (a.size() > 2) {
      vector<int64_t> b;
      for (int i = 0;i+1 < a.size(); ++i) {
        b.push_back(a[i+1] - a[i]);
      }
      a = b;
      int64_t sum = accumulate(a.begin(), a.end(), int64_t(0));
      ans = max({ans, sum, -sum});
    }
    if (n == 1) {
      cout << a[0] << '\n';
    } else {
      ans = max({ans, a[1] - a[0], a[0] - a[1]});
      cout << ans << '\n';
    }
  }
}

Answer: 2


// Created by Rishi Sharma (@rishisharmapro) on 05-02-2025 at 13:10
// #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

void solve() {
    int n;
    cin >> n;
    ll a[n];
    for(int i = 0; i < n; i++) cin >> a[i];

    ll maxSum = accumulate(a, a+n, 0ll), sum = 0;

    while(--n) {
        sum = 0;
        for(int i = 0; i < n; i++) {
            a[i] = a[i+1] - a[i];
            sum += a[i];
        }
        // cout << sum << " ";
        sum = max(sum, -1 * sum);
        maxSum = max(maxSum, sum);
    }
    maxSum = max(maxSum, (ll)a[0]);

    cout << maxSum << endl;
}

int main() {
    // problem: https://codeforces.com/contest/2062/problem/C
    fast_io;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

// Created by Rishi Sharma (@rishisharmapro) on 05-02-2025 at 13:10
// #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

void solve() {
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) cin >> a[i];

    ll maxSum = accumulate(a, a+n, 0ll), sum = 0;

    while(--n) {
        sum = 0;
        for(int i = 0; i < n; i++) {
            a[i] = a[i+1] - a[i];
            sum += a[i];
        }
        // cout << sum << " ";
        sum = max(sum, -1 * sum);
        maxSum = max(maxSum, sum);
    }
    maxSum = max(maxSum, (ll)a[0]);

    cout << maxSum << endl;
}

int main() {
    // problem: https://codeforces.com/contest/2062/problem/C
    fast_io;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


/*Les do some thikr:
سُبْحَانَ ٱللَّٰهِ
ٱلْحَمْدُ لِلَّٰهِ
 الله أكبر
أَسْتَغْفِرُ ٱللَّٰهَ
سُبْحَانَ اللَّهِ وَبِحَمْدِهِ، سُبْحَانَ اللَّهِ العَظِيم
 لَا حَوْلَ وَلَا قُوَّةَ إِلَّا بِٱللَّٰهِ ٱلْعَلِيِّ ٱلْعَظِيمِ
لا إله إلا الله
and durood
اللَّهُمَّ بَارِكْ عَلَى مُحَمَّدٍ، وَعَلَى آلِ مُحَمَّدٍ، كَمَا بَارَكْتَ عَلَى إِبْرَاهِيمَ، وَعَلَى آلِ إِبْرَاهِيمَ، إِنَّكَ حَمِيدٌ مَجِيدٌ
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long

//bitset<100001>a;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
cin>>t;

    while(t--)
    {
      ll n,i,x,f=0,sum=0,mx;
      cin>>n;
     vector<ll>v,v1;
      for(i=0; i<n; i++)
      {
          cin>>x;
         v.push_back(x);

         sum+=x;
      }
     mx=sum;
      while(n--)
      { sum=0,f=0;
          reverse(v.begin(),v.end());
          for(i=1; i<v.size(); i++)
          { x=v[i]-v[i-1];

          sum+=x;
              v1.push_back(x);
              f=1;
          }

          v=v1;
          v1.clear();
          if(f)
         mx=max(mx,sum);
      }
     // cout<<v[0]<<" ";

   cout<<mx<<"\n";
    }

return 0;
}

/*Les do some thikr:
سُبْحَانَ ٱللَّٰهِ
ٱلْحَمْدُ لِلَّٰهِ
 الله أكبر
أَسْتَغْفِرُ ٱللَّٰهَ
سُبْحَانَ اللَّهِ وَبِحَمْدِهِ، سُبْحَانَ اللَّهِ العَظِيم
 لَا حَوْلَ وَلَا قُوَّةَ إِلَّا بِٱللَّٰهِ ٱلْعَلِيِّ ٱلْعَظِيمِ
لا إله إلا الله
and durood
اللَّهُمَّ بَارِكْ عَلَى مُحَمَّدٍ، وَعَلَى آلِ مُحَمَّدٍ، كَمَا بَارَكْتَ عَلَى إِبْرَاهِيمَ، وَعَلَى آلِ إِبْرَاهِيمَ، إِنَّكَ حَمِيدٌ مَجِيدٌ
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long

//bitset<100001>a;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
cin>>t;

    while(t--)
    {
      ll n,i,x,f=0,sum=0,mx;
      cin>>n;
     vector<ll>v,v1;
      for(i=0; i<n; i++)
      {
          cin>>x;
         v.push_back(x);

         sum+=x;
      }
     mx=sum;
      while(n--)
      { sum=0,f=0;

          for(i=1; i<v.size(); i++)
          { x=v[i]-v[i-1];

          sum+=x;
              v1.push_back(x);
              f=1;
          }

          v=v1;
          v1.clear();
          if(f)
         mx=max({mx,sum,-sum});
      }
     // cout<<v[0]<<" ";

   cout<<mx<<"\n";
    }

return 0;
}

Answer: 2


#include<bits/stdc++.h>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#define ll long long int
#define fr(i,j,n) for(i=j;i<n;i++)
#define srt(a) sort(a.begin(),a.end())
#define srtr(a) sort(a.begin(),a.end(),greater<ll>())
#define rvr(a) reverse(a.begin(),a.end())
#define rdp(a) a.resize(unique(a.begin(),a.end())-a.begin())
#define mxel(a) *max_element(a.begin(),a.end())
#define mnel(a) *min_element(a.begin(),a.end())
#define ve vector<ll>
#define vep vector<pair<ll,ll>>
#define vee vector<vector<ll>>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (auto &i : v)
        is >> i;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> v)
{
    for (auto &i : v)
        os << i << ' ';
    return os;
}
const ll M=998244353;
const ll N=4e5+2;
void solve()
{
    ll n;cin >> n;ve a(n);cin >> a;ll i,ans,an=-N;if(n==1) {cout << a[0] << endl;return;}
    while(n){
        ans=0;fr(i,0,n) ans+=a[i];an=max(an,ans);
        fr(i,0,n)a[i]=a[i+1]-a[i];
        if(n==1) if(ans<0) an=max(an,-ans);
        n--;
    }
    cout << an << endl;
}
int main()
{
    ll testcase=1;
    cin >> testcase;
    while(testcase--) solve();
    return 0;
}

#include<bits/stdc++.h>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#define ll long long int
#define fr(i,j,n) for(i=j;i<n;i++)
#define srt(a) sort(a.begin(),a.end())
#define srtr(a) sort(a.begin(),a.end(),greater<ll>())
#define rvr(a) reverse(a.begin(),a.end())
#define rdp(a) a.resize(unique(a.begin(),a.end())-a.begin())
#define mxel(a) *max_element(a.begin(),a.end())
#define mnel(a) *min_element(a.begin(),a.end())
#define ve vector<ll>
#define vep vector<pair<ll,ll>>
#define vee vector<vector<ll>>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (auto &i : v)
        is >> i;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> v)
{
    for (auto &i : v)
        os << i << ' ';
    return os;
}
const ll M=998244353;
const ll N=4e5+2;
ll a[1010];
void solve()
{
    ll n,i;cin >> n;fr(i,1,n+1)cin >> a[i];ll j,k=n,ans,an=-N;//if(n==1) {cout << a[1] << endl;return;}
    fr(i,1,n+1){
        ans=0;fr(j,1,k+1) ans+=a[j];
        if(i==1)an=max(an,ans);
        else an=max(an,max(ans,(-ans)));
        fr(j,1,k)a[j]=(a[j+1]-a[j]);
        //if(n==1) if(ans<0) an=max(an,-ans);
        k--;
    }
    cout << an << endl;
}
int main()
{
    ll testcase=1;
    cin >> testcase;
    while(testcase--) solve();
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define inf (1e9)
#define INF (1e18)
#define eps (1e-8)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ull unsigned long long
#define int long long
#define Int __int128
typedef long long LL;
mt19937 rnd(time(0));
const int N = 5e5 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int n, a[N];

void solve()
{
    vector<int> f1, f2;
    int ans = -1e15;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        f1.push_back(x);
    }
    if (n == 1) {
        cout << f1[0] << endl;
        return;
    }
    int res = 0;
    for (auto x : f1) {
        res += x;
    }
    ans = max(ans, max(res, -res));
    while (f1.size() > 1) {
        f2.clear();
        for (int i = 1; i < f1.size(); i += 1) {
            f2.push_back(f1[i] - f1[i - 1]);
        }
        res = 0;
        for (auto x : f2) res += x;
        ans = max(ans, max(res, -res));
        f1 = f2;
    }
    cout << ans << endl;
}

signed main()
{
	// IOS;
    // init();
	int __;
    cin >> __;
    while (__ --)
    solve();

	return 0;
}
//-Wl,--stack=1998244353

/*

*/

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp(x,y) make_pair(x,y)
#define inf (1e9)
#define INF (1e18)
#define eps (1e-8)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ull unsigned long long
#define int long long
#define Int __int128
typedef long long LL;
mt19937 rnd(time(0));
const int N = 5e5 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int n;

void solve()
{
    vector<int> f1, f2;
    int ans = -1e15;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        f1.push_back(x);
    }
    if (n == 1) {
        cout << f1[0] << endl;
        return;
    }
    int res = 0;
    for (auto x : f1) {
        res += x;
    }
    ans = max(ans, res);
    while (f1.size() > 1) {
        f2.clear();
        for (int i = 1; i < f1.size(); i += 1) {
            f2.push_back(f1[i] - f1[i - 1]);
        }
        res = 0;
        for (auto x : f2) res += x;
        ans = max(ans, max(res, -res));
        f1 = f2;
    }
    cout << ans << endl;
}

signed main()
{
	// IOS;
    // init();
	int __;
    cin >> __;
    while (__ --)
    solve();

	return 0;
}
//-Wl,--stack=1998244353

/*

*/

Answer: 2


#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>

using namespace std;
int main() {
  int num_tests;
  cin >> num_tests;

  for (int t = 0; t < num_tests; ++t) {
    int n;
    cin >> n;
    vector<int> a;
    int s = 0;
    for (int i = 0; i < n; ++i) {
      int ai;
      cin >> ai;
      s += ai;
      a.push_back(ai);
    }

    vector<int> b;
    while (a.size() >= 2) {
      int next_s = 0;
      if (a[0] > a[a.size() - 1]) {
        reverse(a.begin(), a.end());
      }
      for (int i = 1; i < a.size(); ++i) {
        next_s += a[i] - a[i - 1];
        b.push_back(a[i] - a[i - 1]);
      }
      if (next_s > s) {
        s = next_s;
      }
      a = b;
      b.clear();
    }
    printf("%d\n", s);
  }
}

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>

using namespace std;
int main() {
  int num_tests;
  cin >> num_tests;

  for (int t = 0; t < num_tests; ++t) {
    int n;
    cin >> n;
    vector<long long> a;
    long long s = 0;
    for (int i = 0; i < n; ++i) {
      long ai;
      cin >> ai;
      s += ai;
      a.push_back(ai);
    }

    vector<long long> b;
    while (a.size() >= 2) {
      long long next_s = 0;
      if (a[0] > a[a.size() - 1]) {
        reverse(a.begin(), a.end());
      }
      for (int i = 1; i < a.size(); ++i) {
        next_s += a[i] - a[i - 1];
        b.push_back(a[i] - a[i - 1]);
      }
      if (next_s > s) {
        s = next_s;
      }
      a = b;
      b.clear();
    }
    printf("%lld\n", s);
  }
}

Answer: 2


#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> arr(n);
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        if(n==1){
            cout<<arr[1]<<endl;
            continue;
        }
        int temp=n;
        long long ans = -1e18;

        for(int i=1;i<=n;i++){
            long long sum=0;
            for (int i = 1; i <= temp; i++) sum = (sum + arr[i]);
			if (i == 1) ans = max(ans, sum);
			else ans = max(ans, max(sum, ( - sum) ));
			for (int i = 1; i < temp; i++) arr[i] = (arr[i + 1] - arr[i]);
			temp--;
        }

        cout<<abs(ans)<<endl;

    }
    return 0;
}

#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<long long> arr(n+1);
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        if(n==1){
            cout<<arr[1]<<endl;
            continue;
        }
        int temp=n;
        long long ans = -1e18;

        for(int i=1;i<=n;i++){
            long long sum=0;
            for (int i = 1; i <= temp; i++) sum = (sum + arr[i]);
			if (i == 1) ans = max(ans, sum);
			else ans = max(ans, max(sum, ( - sum) ));
			for (int i = 1; i < temp; i++) arr[i] = (arr[i + 1] - arr[i]);
			temp--;
        }

        cout<<abs(ans)<<endl;

    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n; // Number of test cases
    cin >> n;

    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a ;
        vector<ll> k(a);

        for (int i = 0; i < a; i++) {
            cin >> k[i];
        }
        bool flag = true;
        int size = a;
        ll sum = accumulate(k.begin(),k.end(),0ll);
        // if(size == 2) sum = max(sum,max(k[0] - k[1],k[1] - k[0]));
        // else{
        while(size!=1){
                for(int i = 1;i<size;i++){
                    k[i-1] = k[i] - k[i-1];
                }
                k.pop_back();
                ll curr = accumulate(k.begin(),k.end(),0ll);
                sum = max(sum,abs(curr));
                size--;
            }
        // }
        cout << sum << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n; // Number of test cases
    cin >> n;

    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a ;
        vector<ll> k(a);

        for (int i = 0; i < a; i++) {
            cin >> k[i];
        }
        bool flag = true;
        int size = a;
        ll sum = accumulate(k.begin(),k.end(),0ll);
        if(size == 2) sum = max(sum,max(k[0] - k[1],k[1] - k[0]));
        else{
        while(size!=1){
                for(int i = 1;i<size;i++){
                    k[i-1] = k[i] - k[i-1];
                }
                k.pop_back();
                sum = max(sum,abs(k.front() - k.back()));
                size--;
            }
        }
        cout << sum << endl;
    }

    return 0;
}

Answer: 1


#define SETBIT(x, i) ((x & (1 << i)) != 0)
#define ll long long
#include<bits/stdc++.h>
#define inf LLONG_MAX
#define neg_inf LLONG_MIN
using namespace std ;
void solve(){
  int n ;
  cin>>n ;
  vector<ll>arr(n) ;
  int cntneg=0 ;
  for(int i=0 ; i<n ; i++){
    cin>>arr[i] ;
    if(arr[i]<0)cntneg++;
}
  if(n==1){
    cout<<arr[0]<<endl;
    return ;
  }

  ll ans =0 ;
  for(int i=0 ; i<n ; i++)ans+=arr[i] ;

  vector<ll>temp = arr ;
  for(int i=0 ; i<n-1 ; i++){
    vector<ll> k ;
     for(int j=1 ; j<temp.size() ; j++){
      k.push_back(temp[j]-temp[j-1]);
    }
    temp = k ;

    ll S = 0 ;
    for(int i=0 ; i<k.size();i++)S+=k[i] ;
    ans = max(ans , S) ;
  ans = max(ans,-S) ;
  }
  cout<<ans<<endl;

}
int main(){
  ll t ;
  cin>>t ;
  while(t--)solve() ;
  return 0 ;
}

#define SETBIT(x, i) ((x & (1 << i)) != 0)
#define ll long long
#include<bits/stdc++.h>
#define inf LLONG_MAX
#define neg_inf LLONG_MIN
using namespace std ;
void solve(){
  int n ;
  cin>>n ;
  vector<ll>arr(n) ;
  for(int i=0 ; i<n ; i++)cin>>arr[i];
  if(n==1){
    cout<<arr[0]<<endl;
    return ;
  }
  ll ans =accumulate(arr.begin(),arr.end(),0) ;

  ans = max(ans , -ans) ;
  vector<ll>temp = arr ;
  for(int i=0 ; i<n ; i++){
    vector<ll> k ;
     for(int j=0 ; j<temp.size()-1 ; j++){
      k.push_back(temp[j+1]-temp[j]);
    }
    temp = k ;
    ll S = accumulate(k.begin(),k.end(),0) ;
    ans = max(ans , S) ;
    ans = max(ans,-S) ;
  }
  cout<<ans<<endl;

}
int main(){
  ll t ;
  cin>>t ;
  while(t--)solve() ;
  return 0 ;
}

Answer: 1


#include <bits/stdc++.h>

#define max(a, b) ((a > b) ? a : b)

using namespace std;

long long a[59];
int tmp[59];

int t;
int n;

long long res = 0;
long long sum;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> t;

    while (t--)
    {
        cin >> n;

        res = LONG_LONG_MIN;

        for (int i = 0; i < n; i++) cin >> a[i];

        int k = n;

        for (int j = 0; j < k; j++)
        {
            sum = 0;

            for (int i = 0; i < n; i++) sum += a[i];

            res = max(res, sum);
            if (k > 1) res = max(res, -sum);
            n--;

            for (int i = 0; i < n; i++) a[i] = a[i + 1] - a[i];
        }

        cout << res << '\n';
    }

    return 0;
}

#include <bits/stdc++.h>

#define max(a, b) ((a > b) ? a : b)

using namespace std;

long long a[59];
int tmp[59];

int t;
int n;

long long res = 0;
long long sum;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> t;

    while (t--)
    {
        cin >> n;

        res = LONG_LONG_MIN;

        for (int i = 0; i < n; i++) cin >> a[i];

        int k = n;

        for (int j = 0; j < k; j++)
        {
            sum = 0;

            for (int i = 0; i < n; i++) sum += a[i];

            res = max(res, sum);
            if (j > 0) res = max(res, -sum);
            n--;

            for (int i = 0; i < n; i++) a[i] = a[i + 1] - a[i];
        }

        cout << res << '\n';
    }

    return 0;
}

Answer: 2


Answer: 1


#include<bits/stdc++.h>
using  namespace std;

void solve() {
    long long n;
    cin >> n;
    vector<long long> a(n);
    for (long long i = 0;i <n ; i++) {cin >> a[i];}
    if (n == 1){ cout << a[0] << endl; return; }

    long long sum1 = 0;
    long long ans = 0;

    for (long long i = 0; i<n ; i++) {ans += a[i];}

    long long start = 0;
    long long len = n;
    while (len > 0) {
        for (long long j = n-1; j >= start+1; j--) {
            a[j] -= a[j-1];
        }
        sum1 = 0;
        for (long long i = start; i<n ; i++) {sum1 += a[i];}
        ans = max(ans, abs(sum1));
        start++; len--;
    }
    // ans = max(abs(a[start] - a[start+1]), max(abs(a[start+1] - a[start]) , sum1));
    cout << ans << endl;
}

int main() {
    long long t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
using  namespace std;

void solve() {
    long long n;
    cin >> n;
    vector<long long> a(n);
    for (long long i = 0;i <n ; i++) {cin >> a[i];}
    if (n == 1){ cout << a[0] << endl; return; }

    long long sum1 = 0;
    long long ans = 0;

    for (long long i = 0; i<n ; i++) {ans += a[i];}

    long long start = 0;
    long long len = n;
    while (len > 1) {
        for (long long j = n-1; j >= start+1; j--) {
            a[j] -= a[j-1];
        }
        sum1 = 0;
        start++;
        for (long long i = start; i<n ; i++) {sum1 += a[i];}
        ans = max(ans, abs(sum1));
         len--;
    }
    // ans = max(abs(a[start] - a[start+1]), max(abs(a[start+1] - a[start]) , sum1));
    cout << ans << endl;
}

int main() {
    long long t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define ll long long
#define el "\n"
#define OrganicBot ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define MOD 998244353
#define int long long
#define vi vector<int>
#define all(x) (x).begin(), (x).end()

using namespace std;

vector<int> applyDifference(const vector<int>& seq) {
    vector<int> diffSeq;
    for (int i = 1; i < seq.size(); ++i) {
        diffSeq.push_back(seq[i] - seq[i-1]);
    }
    return diffSeq;
}

void solve() {
    int n;
    cin >> n;

    vi seq(n);
    for(int i=0;i<n;i++){
        cin >> seq[i];
    }
    int og = accumulate(all(seq),0ll);

    while (seq.size() > 1) {
        // Step 1: Apply difference operation
        seq = applyDifference(seq);

        // Step 2: Reverse the sequence
        reverse(seq.begin(), seq.end());
    }

    // The final result, which is the only remaining element
    if(n!=1)
    cout<< max(og,abs(seq[0])) << endl;
    else
        cout << og << el;
}

signed main() {
    OrganicBot;
    int t;
    cin >> t;  // Number of test cases

    while (t--) {
        solve();
    }

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define el "\n"
#define OrganicBot ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define MOD 998244353
#define int long long
#define vi vector<int>
#define all(x) (x).begin(), (x).end()

using namespace std;

vector<int> applyDifference(const vector<int>& seq) {
    vector<int> diffSeq;
    for (int i = 1; i < seq.size(); ++i) {
        diffSeq.push_back(seq[i] - seq[i-1]);
    }
    return diffSeq;
}

void solve() {
    int n;
    cin >> n;

    vi seq(n);
    for(int i=0;i<n;i++){
        cin >> seq[i];
    }
    int og = accumulate(all(seq),0ll);
    int ans = og;
    while (seq.size() > 1) {
        // Step 1: Apply difference operation
        seq = applyDifference(seq);
        ans = max(ans,abs(accumulate(all(seq),0ll)));
    }

    // The final result, which is the only remaining element
        cout << max(og,ans) << el;
}

signed main() {
    OrganicBot;
    int t;
    cin >> t;  // Number of test cases

    while (t--) {
        solve();
    }

    return 0;
}

Answer: 2


import sys
data = sys.stdin.read().splitlines()

line = 2
while (line < len(data)):
    arr = list(map(int, data[line].split()))
    # print("arr=", arr)
    new_arr = arr
    diff = sum(arr)
    if len(arr) == 2:
        diff = max(abs(arr[0]-arr[1]), diff)
    else:
        for i in range(len(arr)-1):
            new_arr = [new_arr[i+1]-new_arr[i] for i in range(len(new_arr)-1)]
            if diff < abs(new_arr[0] - new_arr[-1]):
                diff = abs(new_arr[0] - new_arr[-1])
            # print(new_arr, "...diff=", diff)
    print(diff)
    line += 2

import sys
data = sys.stdin.read().splitlines()

line = 2
while (line < len(data)):
    arr = list(map(int, data[line].split()))
    new_arr = arr
    diff = sum(arr)
    # print(arr, "...diff=", diff)
    if len(arr) == 2:
        diff = max(abs(arr[0]-arr[1]), diff)
    elif len(arr) > 2:
        for i in range(len(arr)-2):
            new_arr = [new_arr[i+1]-new_arr[i] for i in range(len(new_arr)-1)]
            diff = max(abs(new_arr[0]-new_arr[-1]), diff, abs(sum(new_arr)))
            # print(new_arr, "...diff=", diff)
    print(diff)
    line += 2

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        vector<long long> v(n);
        long long sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> v[i];
            sum += v[i];
        }
        while (v.size() != 1) {
            vector<long long> v1(v.size() - 1);
            for (int i = 0; i < (int) v.size() - 1; ++i) {
                v1[i] = v[i + 1] - v[i];
            }
            sum = max(sum, abs(accumulate(v1.begin(), v1.end(), 0LL)));
            v = v1;
        }
        cout << sum << '\n';
    }
}

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        vector<long long> v(n);
        long long sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> v[i];
        }
        while (v.size() != 1) {
            vector<long long> v1;
            for (int i = 0; i < (int) v.size() - 1; ++i) {
                v1.push_back(v[i + 1] - v[i]);
            }
            sum = max(sum, abs(accumulate(v1.begin(), v1.end(), 0LL)));
            v = v1;
        }
        cout << sum << '\n';
    }
}

Answer: 1


#include<iomanip>
#include<cstring>
#include<vector>
#include<iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <set>
#include <unordered_map>
using namespace std;

int main() {
	int t;
	cin >> t;
	long long ans = 0;
	for (int l = 0; l < t; l++) {
		int n;
		cin >> n;
		long long ans = -1e18;
		long long sum = 0;
		vector<long long> a;
		a.resize(n);
		for (int i = 0; i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n;i++) {
			long long sum = 0;
			for (int j = 0; j < n - i;j++) {
				sum += a[j];
			}
			if (n == 1 || n == 2) {
				ans = sum;
				continue;
			}
			ans = max(ans, max(sum, -sum));
			for (int j = 0; j < n - i - 1;j++) {
				a[j] = a[j + 1] - a[j];
			}
		}
		cout << ans << endl;

	}

}

#include<iomanip>
#include<cstring>
#include<vector>
#include<iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <set>
#include <unordered_map>
using namespace std;

int main() {
	int t;
	cin >> t;
	long long ans = 0;
	for (int l = 0; l < t; l++) {
		int n;
		cin >> n;
		long long ans = -1e18;
		long long sum = 0;
		vector<long long> a;
		a.resize(n);
		for (int i = 0; i < n;i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n;i++) {
			long long sum = 0;
			for (int j = 0; j < n - i;j++) {
				sum += a[j];
			}
			if (i == 0) {
				ans = max(ans,sum);
			}
			else {
				ans = max(ans, max(sum, -sum));
			}
			for (int j = 0; j < n - i - 1;j++) {
				a[j] = a[j + 1] - a[j];
			}
		}
		cout << ans << endl;

	}

}

Answer: 2


#include <bits/stdc++.h>
#define for1(i,a,b) for(int i=(a); (i)<=(b); ++(i))
#define for2(i,a,b) for(int i=(a); (i)>=(b); --(i))
#define pedl cout<<"\n";
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define of pop_front
#define ob pop_back
#define ls p<<1
#define rs p<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
using namespace std;
mt19937 rnd(time(0));//uint
mt19937_64 rnd_64(time(0));//ull
template<typename T> void cmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void cmax(T& x, T y) {if(x < y) x = y;}
template<typename T> void rin(T& x){
x=0;bool flag(0);char ch=getchar();while(!isdigit(ch)) flag=ch=='-',ch=getchar();
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48),ch=getchar();}flag?x=-x:0;}
template<typename T> void rout(T x,bool flag=0){
x<0?x=-x,putchar('-'):0;static short S[50],tp(0);do S[++tp]=x%10,x/=10;while(x);
while(tp){putchar(S[tp--]|48);}flag?putchar('\n'):putchar(' ');}
template<typename T> T gcd(T x, T y) {return y?gcd(y,x%y):x;}
template<typename T> T lcm(T x, T y) {return x/gcd(x,y)*y;}
typedef double db;
typedef long double ld;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const double eps = 1e-7;
const int base = 13331;
const int N = 1e5+5;
const int M = 1e5+5;
const int IINF = 0x3f3f3f3f;
const LL LINF = 1e18;
const LL mod = 10007;

LL qpow(LL x,LL y,LL Mod=mod) {
	if(y==0) return 1;
	LL res=(x%=Mod); y--; /*y%=(mod-1);*/
	for(;y;y>>=1,x=x*x%Mod)
		if(y&1) res=res*x%Mod;
	return res;
}
template<typename T>
void madd(T& xx,LL y,LL Mod=mod) {
	LL x=1ll*xx;
	if((x+=y)>0) x%=Mod;
	else x+=((-x)/Mod+1)*Mod;
	xx=x%Mod;
}

LL n;
LL a[N];
LL f[2][N];
LL sum,ans;
void solve(){
	cin>>n; sum=ans=0;
	for1(i,1,n) cin>>a[i],ans+=(f[0][i]=a[i]);
	for1(i,1,n-1){
		for1(j,1,n-i)
			sum+=(f[i%2][j]=f[(i%2)^1][j]-f[(i%2)^1][j+1]);
		cmax(ans,abs(sum));
		sum=0;
	}
	cout<<ans<<"\n";
	return ;
}

signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	time_t start=clock();
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int Testcase=1;
cin>>Testcase;
while(Testcase--){
	solve();
}
	time_t duration=clock()-start;
	cerr<<"\ntime="<<duration;
    return 0;
}

#include <bits/stdc++.h>
#define for1(i,a,b) for(int i=(a); (i)<=(b); ++(i))
#define for2(i,a,b) for(int i=(a); (i)>=(b); --(i))
#define pedl cout<<"\n";
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define of pop_front
#define ob pop_back
#define ls p<<1
#define rs p<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
using namespace std;
mt19937 rnd(time(0));//uint
mt19937_64 rnd_64(time(0));//ull
template<typename T> void cmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void cmax(T& x, T y) {if(x < y) x = y;}
template<typename T> void rin(T& x){
x=0;bool flag(0);char ch=getchar();while(!isdigit(ch)) flag=ch=='-',ch=getchar();
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48),ch=getchar();}flag?x=-x:0;}
template<typename T> void rout(T x,bool flag=0){
x<0?x=-x,putchar('-'):0;static short S[50],tp(0);do S[++tp]=x%10,x/=10;while(x);
while(tp){putchar(S[tp--]|48);}flag?putchar('\n'):putchar(' ');}
template<typename T> T gcd(T x, T y) {return y?gcd(y,x%y):x;}
template<typename T> T lcm(T x, T y) {return x/gcd(x,y)*y;}
typedef double db;
typedef long double ld;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const double eps = 1e-7;
const int base = 13331;
const int N = 1e5+5;
const int M = 1e5+5;
const int IINF = 0x3f3f3f3f;
const LL LINF = 1e18;
const LL mod = 10007;

LL qpow(LL x,LL y,LL Mod=mod) {
	if(y==0) return 1;
	LL res=(x%=Mod); y--; /*y%=(mod-1);*/
	for(;y;y>>=1,x=x*x%Mod)
		if(y&1) res=res*x%Mod;
	return res;
}
template<typename T>
void madd(T& xx,LL y,LL Mod=mod) {
	LL x=1ll*xx;
	if((x+=y)>0) x%=Mod;
	else x+=((-x)/Mod+1)*Mod;
	xx=x%Mod;
}

int n;
int a[N];
int f[2][N];
LL sum,ans;
void solve(){
	cin>>n; sum=ans=0;
	for1(i,1,n) cin>>a[i],ans+=(f[0][i]=a[i]);
	for1(i,1,n-1){
		for1(j,1,n-i)
			sum+=(f[i%2][j]=f[(i%2)^1][j]-f[(i%2)^1][j+1]);
		cmax(ans,abs(sum));
		sum=0;
	}
	cout<<ans<<"\n";
	return ;
}

signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	time_t start=clock();
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int Testcase=1;
cin>>Testcase;
while(Testcase--){
	solve();
}
	time_t duration=clock()-start;
	cerr<<"\ntime="<<duration;
    return 0;
}

Answer: 1


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>

using namespace std;

class Newbie {
public:
    void solveOne() {

    }

    void solveTwo() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            long long A[n];
            long long ans = 0;
            for (long long &x: A) {
                cin >> x;
                ans += x;
            }
            int tmpN = n;
            while (tmpN > 1) {
                reverse(A, A + tmpN);
                long long sum = 0;
                for (int i = 0; i < tmpN - 1; ++i) {
                    A[i] = (A[i + 1] - A[i]);
                }
                tmpN--;
                sum = accumulate(A, A + tmpN, 0LL);
                ans = max({ans, sum, -sum});
            }
            cout << ans << "\n";
        }
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout.tie(nullptr);
    Newbie solver;
    solver.solveTwo();
    return 0;
}

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>

using namespace std;

class Newbie {
public:
    void solveOne() {

    }

    void solveTwo() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vector<int> A(n);
            long long ans = 0;
            for (int &x: A) {
                cin >> x;
                ans += x;
            }
            while (A.size() > 1) {
                reverse(A.begin(), A.end());
                long long sum = 0;
                for (int i = 0; i < A.size() - 1; ++i) {
                    A[i] = (A[i + 1] - A[i]);
                }
                A.pop_back();
                sum = accumulate(A.begin(), A.end(), 0LL);
                ans = max({ans, sum, -sum});
            }
            cout << ans << "\n";
        }
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout.tie(nullptr);
    Newbie solver;
    solver.solveTwo();
    return 0;
}

Answer: 1


#include "bits/stdc++.h"
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define int long long

using namespace std;
using ll = long long;
using pii = pair<int, int>;

const char nl = '\n';
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;

void solve() {
	int n; cin >> n;
	vector<int> v(n); for (auto &x : v) cin >> x;

	int ans = accumulate(all(v), 0);

	while (sz(v) > 1) {
		ans = max(ans, abs(v[0] - v.back()));

		for (int i = 0; i < sz(v) - 1; i++)
			v[i] = v[i + 1] - v[i];
		v.pop_back();

		ans = max(ans, abs(v[0] - v.back()));
	}

	cout << ans << nl;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0);

	int t; cin >> t;
	while(t--) {
		solve();
	}

	return 0;
}

#include "bits/stdc++.h"
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define int long long

using namespace std;
using ll = long long;
using pii = pair<int, int>;

const char nl = '\n';
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;

void solve() {
	int n; cin >> n;
	vector<int> v(n); for (auto &x : v) cin >> x;

	int ans = accumulate(all(v), 0LL);

	while (sz(v) > 1) {
		for (int i = 0; i < sz(v) - 1; i++)
			v[i] = v[i + 1] - v[i];
		v.pop_back();

		ans = max(ans, abs(v[0] - v.back()));
	}

	cout << ans << nl;
}

int32_t main() {
	ios::sync_with_stdio(0), cin.tie(0);

	int t; cin >> t;
	while(t--) {
		solve();
	}

	return 0;
}

Answer: 1


#include<iostream>

#include<algorithm>

#include<iomanip>

#include<cmath>

#include<math.h>

#include <vector>

#include <map>

#include <set>

#include<string>

#include <cstring>

#include <stack>

#include<queue>

using namespace std;

#define int long long

typedef pair<int,int> PII;

typedef vector<int> vi;

typedef vector<char> vc;

const int N =2e5+10, INF = 0x3f3f3f3f,mod=1e9+7;

int a[N],b[N];

int dx[]={-1,-1,1,1};

int dy[]={1,-1,1,-1};

void solve() {

	int n,l,r;

	cin>>n>>l>>r;

	int idx1 = 1,idx = 1;

	for(int i = 1;i<=n;i++){

		int x;cin>>x;

		if(i<=r){

			a[idx1++] = x;

		}

		if(i>=l){

			b[idx++] = x;

		}

	}

	int len = r-l+1;

	sort(a+1,a+idx1);

	sort(b+1,b+idx);

	int sum1 = 0,sum2 = 0;

	for(int i = 1;i<=len;i++){

		sum1+=a[i];

		sum2+=b[i];

	}

	cout<<min(sum1,sum2)<<'\n';

}

signed main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	int t=1;

	cin>>t;

	while(t--){

		solve();

	}

    return 0;

}

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<cmath>

#include<math.h>

#include <vector>

#include <map>

#include <set>

#include<string>

#include <cstring>

#include <stack>

#include<queue>

using namespace std;

#define int long long

typedef pair<int,int> PII;

typedef vector<int> vi;

typedef vector<char> vc;

const int N =2e5+10, INF = 0x3f3f3f3f,mod=1e9+7;

int a[N];

int dx[]={-1,-1,1,1};

int dy[]={1,-1,1,-1};

void solve() {

	int n,l,r;

	cin>>n>>l>>r;

	for(int i = 1;i<=n;i++){

		cin>>a[i];

	}

	int len = r-l+1;

	if(l==r){

		cout<<0<<'\n';

		return ;

	}

	sort(a+1,a+1+n);

	int sum = 0;

	for(int i = 1;i<=len;i++){

		sum+=a[i];

	}

	cout<<sum<<'\n';

}

signed main(){

	ios::sync_with_stdio(false);

	cin.tie(0);

	int t=1;

	cin>>t;

	while(t--){

		solve();

	}

    return 0;

}

Answer: 1


#include <bits/stdc++.h>

#define vi vector<int>

#define vll vector<ll>

#define vvi vector<vector<int>>

#define vvii vector<vector<pair<int,int>>>

#define it vector<int>::iterator

#define ii pair<int,int>

#define fori(i,ini,fim) for(int i=ini;i<fim;i++)

#define s second

#define f first

#define pb push_back

#define all(x) x.begin(),x.end()

#define teto(x,y) ((x+y-1)/y)

#define dbgv(v) cout<< #v << " = ";for(auto e:v)cout<<e<<",";cout<<endl

#define dbg(x) cout << #x << " = " << x <<endl

typedef long long ll;

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 2e6+10;

const int MOD = 26;

using namespace std;

void fun(){

    ll n, l, r; cin >> n >> l >> r;

	vll v1, v2;

	fori( i, 0, n ){

		ll a; cin >> a;

		if( i < r ) v1.pb(a);

		if( l <=l ) v2.pb(a);

	}

	sort(all(v1));

	sort(all(v2));

	ll sumv1 = 0;

	ll sumv2 = 0;

	fori( i, 0, r - l + 1 ) sumv1 += v1[i];

	fori( i, 0, r - l + 1 ) sumv2 += v2[i];

	cout << min( sumv1, sumv2 ) << endl;

}

int main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);

	int TC;cin>>TC;

	while (TC--)fun();

	return 0;

}

#include <bits/stdc++.h>

#define vi vector<int>

#define vll vector<ll>

#define vvi vector<vector<int>>

#define vvii vector<vector<pair<int,int>>>

#define it vector<int>::iterator

#define ii pair<int,int>

#define fori(i,ini,fim) for(int i=ini;i<fim;i++)

#define s second

#define f first

#define pb push_back

#define all(x) x.begin(),x.end()

#define teto(x,y) ((x+y-1)/y)

#define dbgv(v) cout<< #v << " = ";for(auto e:v)cout<<e<<",";cout<<endl

#define dbg(x) cout << #x << " = " << x <<endl

typedef long long ll;

const int INF = 0x3f3f3f3f;

const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const int MAX = 2e6+10;

const int MOD = 26;

using namespace std;

void fun(){

    ll n, l, r; cin >> n >> l >> r;

	vll v1, v2;

	fori( i, 0, n ){

		ll a; cin >> a;

		if( i <= r -1 ) v1.pb(a);

		if( l - 1 <=i ) v2.pb(a);

	}

	sort(all(v1));

	sort(all(v2));

	ll sumv1 = 0;

	ll sumv2 = 0;

	fori( i, 0, r - l + 1 ) sumv1 += v1[i];

	fori( i, 0, r - l + 1 ) sumv2 += v2[i];

	cout << min( sumv1, sumv2 ) << endl;

}

int main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);

	int TC;cin>>TC;

	while (TC--)fun();

	return 0;

}

Answer: 2


#include <bits/stdc++.h>

#define ll long long

using namespace std;

void solve(){

    ll n,l,r;

    cin>>n>>l>>r;

    vector<ll>a(n);

    for(ll i=0;i<n;i++){

        cin>>a[i];

    }

   vector<ll>v1(a.begin(),a.begin()+r);

   vector<ll>v2(a.begin()+l-1,a.end());

   sort(v1.begin(),v1.end());

   sort(v2.begin(),v2.end());

    ll d=(r-l+1);

    ll sum1=0;

    ll sum2=0;

    for(ll i=0;i<d;i++){

        sum1 +=v1[i];

        sum2 +=v2[i];

    }

    cout<<min(sum1,sum2)<<endl;

}

int main() {

    ll t;

    cin>>t;

    while(t--){

        solve();

    }

}

#include <bits/stdc++.h>

#define ll long long

using namespace std;

void solve(){

    ll n,l,r;

    cin>>n>>l>>r;

    vector<ll>a(n);

    for(ll i=0;i<n;i++){

        cin>>a[i];

    }

    sort(a.begin(),a.end());

    ll d=(r-l+1);

    ll sum=0;

    for(ll i=0;i<d;i++){

        sum +=a[i];

    }

    cout<<sum<<endl;

}

int main() {

    ll t;

    cin>>t;

    while(t--){

        solve();

    }

}

Answer: 1


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

#define ff first

#define ss second

#define int long long

#define pb push_back

#define mp make_pair

#define pii pair<int, int>

#define vi vector<int>

#define mii map<int, int>

#define pqb priority_queue<int>

#define pqs priority_queue<int, vi, greater<int>>

#define setbits(x) __builtin_popcountll(x)

#define zrobits(x) __builtin_ctzll(x)

#define mod 100000007

#define inf 1e18

#define ps(x, y) fixed << setprecision(y) << x

#define mk(arr, n, type) type *arr = new type[n]

#define w(x)  \

    int x;    \

    cin >> x; \

    while (x--)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void c_p_c()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

}

int32_t main()

{

    c_p_c();

    w(x)

    {

        int n;

        int l;

        int r;

        cin >> n >> l >> r;

        vi arr(n);

        for (int i = 0; i < n; i++)

            cin >> arr[i];

        vi left_seg;

        vi right_seg;

        vi seg;

        for (int i = l - 1; i < r; i++)

        {

            seg.pb(arr[i]);

        }

        for (int i = 0; i < l - 1; i++)

        {

            left_seg.pb(arr[i]);

        }

        for (int i = r; i < n; i++)

        {

            right_seg.pb(arr[i]);

        }

        sort(seg.begin(), seg.end());

        sort(left_seg.begin(), left_seg.end());

        sort(right_seg.begin(), right_seg.end());

        vi temp = seg;

        int left = 0;

        int s = seg.size() - 1;

        while (s >= 0 && left < left_seg.size() && left_seg[left] < seg[s])

        {

            seg[s] = left_seg[left];

            s--;

            left++;

        }

        int answer_left = accumulate(seg.begin(), seg.end(), 0LL);

        int right = 0;

        s = temp.size() - 1;

        while (s >= 0 && right < right_seg.size() && right_seg[right] < temp[s])

        {

            temp[s] = right_seg[right];

            s--;

            right++;

        }

        int answer_right = accumulate(temp.begin(), temp.end(), 0LL);

        int final_answer = min(answer_left, answer_right);

        cout << final_answer << endl;

    }

    return 0;

}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 100000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define w(x)  \
    int x;    \
    cin >> x; \
    while (x--)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void c_p_c()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int32_t main()
{
    c_p_c();
    w(x)
    {
        int n;
        int l;
        int r;
        cin >> n >> l >> r;
        vi arr(n);
        for (int i = 0; i < n; i++)
            cin >> arr[i];
        vi left_seg;
        vi right_seg;
        vi seg;
        for (int i = l - 1; i < r; i++)
        {
            seg.pb(arr[i]);
        }
        for (int i = 0; i < l - 1; i++)
        {
            left_seg.pb(arr[i]);
        }
        for (int i = r; i < n; i++)
        {
            right_seg.pb(arr[i]);
        }

        sort(seg.begin(), seg.end());
        sort(left_seg.begin(), left_seg.end());
        sort(right_seg.begin(), right_seg.end());
        vi temp = seg;
        int left = 0;
        int s = seg.size() - 1;
        while (s >= 0 && left < left_seg.size() && left_seg[left] < seg[s])
        {
            seg[s] = left_seg[left];
            s--;
            left++;
        }
        int answer_left = accumulate(seg.begin(), seg.end(), 0);

        int right = 0;
        s = temp.size() - 1;
        while (s >= 0 && right < right_seg.size() && right_seg[right] < temp[s])
        {
            temp[s] = right_seg[right];
            s--;
            right++;
        }
        int answer_right = accumulate(temp.begin(), temp.end(), 0);
        int final_answer = min(answer_left, answer_right);

        cout << final_answer << endl;
    }
    return 0;
}

Answer: 1


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using ll=long long;
using ld=long double;
using namespace std;

int main() {
    int t;cin>>t;
    while(t--){
        int n,l,r;cin>>n>>l>>r;
        vector<int> v(n);
        for(int i=0;i<n;i++){
            cin>>v[i];
        }
        sort(v.begin(),v.end());
        ll ans=0;
        for(int i=0;i<=r-l;i++){
            ans+=v[i];

        }
        cout<<ans<<endl;
    }
}

#include <iostream>

#include <vector>

#include <algorithm>

#include <cmath>

#include <queue>

using ll=long long;

using ld=long double;

using namespace std;

int main() {

    int t;cin>>t;

    while(t--){

        int n,l,r;cin>>n>>l>>r;

        l--;

        vector<ll> v(n);

        for(int i=0;i<n;i++){

            cin>>v[i];

        }

        vector<ll> brr = v, crr = v;

        sort(brr.begin()+l,brr.end());

        sort(crr.begin(),crr.begin()+r,greater<ll>());

        ll ans1=0,ans2=0;

        for(int i=l;i<r;i++){

            ans1+=brr[i];

            ans2+=crr[i];

        }

        cout<<min(ans1,ans2)<<endl;

    }

}

Answer: 2


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    int t;
    cin>>t;
    while(t--){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    int sum1=0;
    int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    int t;
    cin>>t;
    while(t--){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    long long int sum1=0;
    long long int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1 && j>=l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n && i>=l-1){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    long long int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main(){

    int k;

    cin >> k;

    while(k--){

    int n,l,r;

    cin >> n >> l >> r;

    vector<long long int>arr1(n,0);

    for(int i=0;i<n;i++)cin >> arr1[i];

    priority_queue<long long int,vector<long long int>,greater<long long int>>q;

    for(int i=0;i<r;i++){

        q.push(arr1[i]);

    }

    long long int sum = 0;

    long long int sum1 = 0;

    for(int i=0;i<r-l+1;i++){

        sum+=q.top();

        q.pop();

    }

    while(!q.empty())q.pop();

    for(int i=l-1;i<n;i++){

        q.push(arr1[i]);

    }

    for(int i=0;i<r-l+1;i++){

        sum1+=q.top();

        q.pop();

    }

    cout << min(sum,sum1) << endl;

    }

}

#include <bits/stdc++.h>

using namespace std;

int main(){

    int k;

    cin >> k;

    while(k--){

    int n,l,r;

    cin >> n >> l >> r;

    vector<long long int>arr1(n,0);

    for(int i=0;i<n;i++)cin >> arr1[i];

    priority_queue<long long int,vector<long long int>,greater<long long int>>q;

    for(int i=0;i<r;i++){

        q.push(arr1[i]);

    }

    long long int sum = 0;

    long long int sum1 = 0;

    for(int i=0;i<r-l+1;i++){

        sum+=q.top();

        q.pop();

    }

    for(int i=l-1;i<n;i++){

        q.push(arr1[i]);

    }

    for(int i=0;i<r-l+1;i++){

        sum1+=q.top();

        q.pop();

    }

    cout << min(sum,sum1) << endl;

    }

}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

void solve(){

    long long n,l,r;

    long long ans=0,s=0;

    cin>>n>>l>>r;

    int a[n],c[n];

    for(int i=0;i<n;i++){

	cin>>a[i];

	c[i]=a[i];

}

    sort(a,a+r);

    sort(c+l-1,c+n);

    for(int i=0;i<r-l+1;i++){

    	s+=a[i];

	}

    for(int i=l-1;i<r;i++){

        ans+=c[i];

    }

    cout<<min(ans,s)<<endl;

}

int main(){

	int T;

	cin>>T;

	while(T--)solve();

    return 0;

}

#include<bits/stdc++.h>

using namespace std;

void solve(){

    long long n,l,r;

    long long ans=0,s=0;

    cin>>n>>l>>r;

    int a[n],c[n];

    for(int i=0;i<n;i++){

	cin>>a[i];

	c[i]=a[i];

}

    sort(a,a+r);

    sort(c+l-1,c+n);

    for(int i=0;i<r-l+1;i++){

    	s+=a[i];

    	ans+=c[i];

	}

    cout<<min(ans,s)<<endl;

}

int main(){

	int T;

	cin>>T;

	while(T--)solve();

    return 0;

}

Answer: 1


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		vector<int> a(n + 1);
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		auto b = a;
		LL  sum1 = 0, sum2 = 0;
		sort(a.begin() + 1, a.begin() + r + 1);
		for (int i = 1; i <= r-l+1; i++) {
			sum1 += a[i];
		}
		sort(b.begin()  + l, b.end());
		for (int i = l; i <= r; i++) {
			sum2 += b[i];
		}
		//cout << sum1 <<' '<< sum2;
		LL ans = min(sum1, sum2);
		cout << ans << endl;
	}

	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		auto b = a;
		int sum1 = 0, sum2 = 0;
		sort(a.begin(), a.begin() + r-1);
		for (int i = 0; i < r-l; i++) {
			sum1 += a[i];
		}
		sort(b.begin()  + l-1, b.end());
		for (int i = l-1; i < r; i++) {
			sum2 += b[i];
		}
		//cout << sum1 <<' '<< sum2;
		int ans = min(sum1, sum2);
		cout << ans << endl;
	}

	return 0;
}

Answer: 1


#include<bits/stdc++.h>

#define ll long long

#define vi vector<int>

#define vvi vector<vector<int>>

#define vc vector<char>

#define fi for(int i=0;i<n;i++)

#define fit(a) for(auto it:a)

#define pb push_back

#define pf push_front

#define mi map<int,int>

#define umi unordered_map<int,int>

#define usi unordered_set<int>

#define maxheapint priority_queue<int>

#define maxheappair priority_queue<pair<int,int>>

#define minheapint priority_queue<int,vector<int>,greater<int>>

#define minheappair priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>

using namespace std;

void print(vector<int> &a){

    for(auto i:a) cout<<i<<" ";

    cout<<"\n";

}

int main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--){

        ll n,l,r;

        cin>>n>>l>>r;

        vector<ll> a(n);

        fi cin>>a[i];

        vector<ll> b(a.begin()+l-1,a.begin()+r);

        ll ans=accumulate(b.begin(),b.end(),0LL);

        sort(b.begin(),b.end(),greater<ll>());

        vector<ll> left,right;

        ll ans1=ans,ans2=ans;

        for(ll i=0;i<l-1;i++){

            left.push_back(a[i]);

        }

        for(ll i=r;i<n;i++){

            right.push_back(a[i]);

        }

        sort(left.begin(),left.end());

        sort(right.begin(),right.end());

        vector<ll> left1,right1;

        ll szl=left.size(),szr=right.size();

        for(ll i=0;i<min(szl,r-l+1);i++) left1.push_back(left[i]);

        for(ll i=0;i<min(szr,r-l+1);i++) right1.push_back(right[i]);

        ll z=0,s=0;

        while(z<left1.size() && s<b.size() && left1[z]<b[s]){

            ans1+=left1[z++];

            ans1-=b[s++];

        }

        z=0,s=0;

        while(z<right1.size() && s<b.size() && right1[z]<b[s]){

            ans2+=right1[z++];

            ans2-=b[s++];

        }

        cout<<min(ans1,ans2)<<"\n";

    }

}

#include<bits/stdc++.h>

#define ll long long

#define vi vector<int>

#define vvi vector<vector<int>>

#define vc vector<char>

#define fi for(int i=0;i<n;i++)

#define fit(a) for(auto it:a)

#define pb push_back

#define pf push_front

#define mi map<int,int>

#define umi unordered_map<int,int>

#define usi unordered_set<int>

#define maxheapint priority_queue<int>

#define maxheappair priority_queue<pair<int,int>>

#define minheapint priority_queue<int,vector<int>,greater<int>>

#define minheappair priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>

using namespace std;

void print(vector<int> &a){

    for(auto i:a) cout<<i<<" ";

    cout<<"\n";

}

int main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--){

        ll n,l,r;

        cin>>n>>l>>r;

        vector<ll> a(n);

        fi cin>>a[i];

        vector<ll> b(a.begin()+l-1,a.begin()+r);

        ll ans=accumulate(b.begin(),b.end(),0);

        sort(b.begin(),b.end(),greater<ll>());

        vector<ll> left,right;

        ll ans1=ans,ans2=ans;

        for(ll i=0;i<l-1;i++){

            left.push_back(a[i]);

        }

        for(ll i=r;i<n;i++){

            right.push_back(a[i]);

        }

        sort(left.begin(),left.end());

        sort(right.begin(),right.end());

        vector<ll> left1,right1;

        ll szl=left.size(),szr=right.size();

        for(ll i=0;i<min(szl,r-l+1);i++) left1.push_back(left[i]);

        for(ll i=0;i<min(szr,r-l+1);i++) right1.push_back(right[i]);

        ll z=0,s=0;

        while(z<left1.size() && s<b.size() && left1[z]<b[s]){

            ans1+=left1[z++];

            ans1-=b[s++];

        }

        z=0,s=0;

        while(z<right1.size() && s<b.size() && right1[z]<b[s]){

            ans2+=right1[z++];

            ans2-=b[s++];

        }

        cout<<min(ans1,ans2)<<"\n";

    }

}

Answer: 1


#include<bits/stdc++.h>

#define ll long long

using namespace std;

ll func(){

 ll n,l,r;

 cin>>n>>l>>r;

 vector<ll>v(n);

 for(ll i=0;i<n;i++){

     cin>>v[i];

 }

 vector<ll>s(n);

 for(ll i=0;i<n;i++){

     s[i]=v[i];

 }

sort(v.begin(),v.begin()+r);

sort(s.begin()+l-1LL,s.end());

ll sum1=0,sum2=0;

for(ll i=0;i<r-l+1;i++){

    sum1+=v[i];

    }

    for(ll i=l-1;i<r;i++){

        sum2+=s[i];

    }

    return min(sum1,sum2);

}

int main(){

    ll t;cin>>t;

    while(t--){

        cout<<func()<<endl;

    }

}

#include<bits/stdc++.h>

#define ll long long

using namespace std;

int func(){

 ll n,l,r;

 cin>>n>>l>>r;

 vector<ll>v(n);

 for(int i=0;i<n;i++){

     cin>>v[i];

 }

 vector<ll>s(n);

 for(int i=0;i<n;i++){

     s[i]=v[i];

 }

sort(v.begin(),v.begin()+r);

sort(s.begin()+l-1,s.end());

ll sum1=0,sum2=0;

for(int i=0;i<r-l+1;i++){

    sum1+=v[i];

    }

    for(int i=l-1;i<r;i++){

        sum2+=s[i];

    }

    return min(sum1,sum2);

}

int main(){

    ll t;cin>>t;

    while(t--){

        cout<<func()<<endl;

    }

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define F(i, n) for (int i = 0; i < n; i++)

#define vi vector<int>

#define ln long long int

#define test int t; cin >> t; while (t--)

#define ll long long

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x << " -> "; _print(x); cerr << endl

#else

#define debug(x)

#endif

void _print(int a) { cerr << a << " "; }

void _print(long long a) { cerr << a << " "; }

void _print(char a) { cerr << a << " "; }

void _print(string a) { cerr << a << " "; }

void _print(bool a) { cerr << a << " "; }

template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }

template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(pair<T, T> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }

template <class T, class V> void _print(multimap<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(unordered_set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(unordered_multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T, class V> void _print(unordered_map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

void solve(){

  int n,l,r; cin >> n >> l >> r;

  vector<int>v(n);

  for(int i = 0; i<n; i++) cin >> v[i];

  l--,r--;

  vector<int>left;

  vector<int>right;

  for(int i = 0; i<=r; i++){

    left.push_back(v[i]);

  }

  for(int i = l; i<n; i++){

    right.push_back(v[i]);

  }

  sort(left.begin(),left.end());

  sort(right.begin(),right.end());

  // int sum1 = accumulate(left.begin(),left.begin()+ (int)(r-l+1),0LL);

  // int sum2 = accumulate(right.begin(),right.begin()+ (int)(r-l+1),0LL);

  int sum1 = 0;

  int sum2 = 0;

  for(int i = 0; i<r-l+1; i++){

    sum1+=left[i];

  }

  for(int i = 0; i<r-l+1; i++){

    sum2+=right[i];

  }

  cout << min(sum1,sum2) << endl;

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("input.txt","r",stdin);

    freopen("output.txt","w",stdout);

    freopen("Error.txt", "w", stderr);

#endif

test{

   solve();

}

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define F(i, n) for (int i = 0; i < n; i++)

#define vi vector<int>

#define ln long long int

#define test int t; cin >> t; while (t--)

#define ll long long

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x << " -> "; _print(x); cerr << endl

#else

#define debug(x)

#endif

void _print(int a) { cerr << a << " "; }

void _print(long long a) { cerr << a << " "; }

void _print(char a) { cerr << a << " "; }

void _print(string a) { cerr << a << " "; }

void _print(bool a) { cerr << a << " "; }

template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }

template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(pair<T, T> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }

template <class T, class V> void _print(multimap<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(unordered_set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T> void _print(unordered_multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }

template <class T, class V> void _print(unordered_map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

void solve(){

  int n,l,r; cin >> n >> l >> r;

  vector<int>v(n);

  for(int i = 0; i<n; i++) cin >> v[i];

  l--,r--;

  vector<int>left;

  vector<int>right;

  for(int i = 0; i<=r; i++){

    left.push_back(v[i]);

  }

  for(int i = l; i<n; i++){

    right.push_back(v[i]);

  }

  sort(left.begin(),left.end());

  sort(right.begin(),right.end());

  // int sum1 = accumulate(left.begin(),left.begin()+ (int)(r-l+1),0LL);

  // int sum2 = accumulate(right.begin(),right.begin()+ (int)(r-l+1),0LL);

  ll sum1 = 0;

  ll sum2 = 0;

  for(int i = 0; i<r-l+1; i++){

    sum1+=left[i];

  }

  for(int i = 0; i<r-l+1; i++){

    sum2+=right[i];

  }

  cout << min(sum1,sum2) << endl;

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("input.txt","r",stdin);

    freopen("output.txt","w",stdout);

    freopen("Error.txt", "w", stderr);

#endif

test{

   solve();

}

    return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define inv(x) ksm(x, mod - 2)

#define int long long

#define NO cout << "NO" << endl

#define YES cout << "YES" << endl

#define No cout << "No" << endl

#define Yes cout << "Yes" << endl

#define lowbit(x) ((x) & (-x))

#define mem(a, b) memset(a, b, sizeof(a))

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

// const int mod = 998244353;

const int mod = 1e9 + 7;

const int inf = 1e18;

double logn(int n,double x){return log(x)/log(n);}

int ksm(int a, int b){int ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;}

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<pair<int, int>, int> piii;

const int N = 2e5 + 5;

int dx[4] = {0, 1, -1, 0};

int dy[4] = {1, 0, 0, -1};

void solve()

{

	int n;

	cin >> n;

	int l, r;

	cin >> l >> r;

	vector<int> a(n + 1);

	for (int i = 1; i <= n;i++)

		cin >> a[i];

	sort(a.begin() + 1, a.end());

	int g = r - l + 1;

	int sum = 0;

	for (int i = 1; i <= g;i++)

		sum += a[i];

	cout << sum << endl;

}

signed main(){

	cin.tie(0);

	cout.tie(0);

	ios::sync_with_stdio(false);

	int t;

	t = 1;

	cin >> t;

	while (t--)

		solve();

}

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define inv(x) ksm(x, mod - 2)

#define int long long

#define NO cout << "NO" << endl

#define YES cout << "YES" << endl

#define No cout << "No" << endl

#define Yes cout << "Yes" << endl

#define lowbit(x) ((x) & (-x))

#define mem(a, b) memset(a, b, sizeof(a))

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

// const int mod = 998244353;

const int mod = 1e9 + 7;

const int inf = 1e18;

double logn(int n,double x){return log(x)/log(n);}

int ksm(int a, int b){int ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;}

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<pair<int, int>, int> piii;

const int N = 2e5 + 5;

int dx[4] = {0, 1, -1, 0};

int dy[4] = {1, 0, 0, -1};

void solve()

{

	int n;

	cin >> n;

	int l, r;

	cin >> l >> r;

	vector<int> a(n + 1);

	for (int i = 1; i <= n;i++)

		cin >> a[i];

	priority_queue<int> s;

	for (int i = l; i <= r;i++)

		s.push(a[i]);

	for (int i = 1; i < l;i++)

	{

		if(s.top()>a[i])

			s.pop(), s.push(a[i]);

	}

	int minn = 0;

	while(s.size())

		minn += s.top(), s.pop();

		for (int i = l; i <= r;i++)

		s.push(a[i]);

		for (int i = r + 1;i<=n;i++)

		if(s.top()>a[i])

			s.pop(), s.push(a[i]);

		int sum = 0;

		while(s.size())

			sum += s.top(), s.pop();

		minn = min(minn, sum);

		cout << minn << endl;

}

signed main(){

	cin.tie(0);

	cout.tie(0);

	ios::sync_with_stdio(false);

	int t;

	t = 1;

	cin >> t;

	while (t--)

		solve();

}

Answer: 2


#include <iostream>
#include <algorithm>
#include <cstring>
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;

void solve(){
    int n, l, r;
    cin >> n >> l >> r;
    int a[N], b[N];
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(a + 1, a + 1 + r);
    sort(b + l, b + 1 + n);
    long long res1 = 0, res2 = 0;
    for(int i = 1; i <= r - l + 1; i++){
        res1 += a[i];
    }
    for(int i = l; i <= r; i++){
        res2 += b[i];
    }
    cout << min(res1, res2);
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

#include <iostream>
#include <algorithm>
#include <cstring>
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;

void solve(){
    int n, l, r;
    cin >> n >> l >> r;
    int a[N], b[N];
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        b[i] = a[i];
    }
    sort(a + 1, a + 1 + r);
    sort(b + l, b + 1 + n);
    long long res1 = 0, res2 = 0;
    for(int i = 1; i <= r - l + 1; i++){
        res1 += a[i];
    }
    for(int i = l; i <= r; i++){
        res2 += b[i];
    }
    cout << min(res1, res2) << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, l, r;
    cin >> n >> l >> r;
    vector<int> a;
    for(int i = 0; i < n; i++) {
        int u;
        cin >> u;
        a.push_back(u);
    }

    sort(a.begin(), a.begin() + l - 1);
    sort(a.begin() + l - 1, a.begin() + r);
    sort(a.begin() + r, a.end());

    int sum1 = 0, sum2 = 0, sum3 = 0;
    int k1 = 0, k2 = r - 1, k3 = r;
    int change;

    while(k1 <= l - 2 && k2 >= l - 1 && a[k1] < a[k2]) {
        sum1 += a[k1++];
        sum2 += a[k2--];
    }

    change = sum2 - sum1;
    k2 = r - 1;
    sum2 = 0;

    while(k3 <= n - 1 && k2 >= l - 1 && a[k3] < a[k2]) {
        sum3 += a[k3++];
        sum2 += a[k2--];
    }

    change = max(change, sum2 - sum3);

    int result = 0;
    for(int i = l - 1; i <= r - 1; i++) {
        result += a[i];
    }
    result -= change;
    cout << result << endl;
}

int main(){
    int t;
    cin >> t;
    while(t > 0) {
        solve();
        t--;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n, l, r;
    cin >> n >> l >> r;
    vector<int> a;
    for(int i = 0; i < n; i++) {
        int u;
        cin >> u;
        a.push_back(u);
    }

    sort(a.begin(), a.begin() + l - 1);
    sort(a.begin() + l - 1, a.begin() + r);
    sort(a.begin() + r, a.end());

    long long int sum1 = 0, sum2 = 0, sum3 = 0;
    int k1 = 0, k2 = r - 1, k3 = r;
    long long int change;

    while(k1 <= l - 2 && k2 >= l - 1 && a[k1] < a[k2]) {
        sum1 += a[k1++];
        sum2 += a[k2--];
    }

    change = sum2 - sum1;
    k2 = r - 1;
    sum2 = 0;

    while(k3 <= n - 1 && k2 >= l - 1 && a[k3] < a[k2]) {
        sum3 += a[k3++];
        sum2 += a[k2--];
    }

    change = max(change, sum2 - sum3);

    long long int result = 0;
    for(int i = l - 1; i <= r - 1; i++) {
        result += a[i];
    }
    result -= change;
    cout << result << endl;
}

int main(){
    int t;
    cin >> t;
    while(t > 0) {
        solve();
        t--;
    }
    return 0;
}

Answer: 2


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class SubsequenceUpdate {
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    int tests = Integer.parseInt(scanner.nextLine());
    while (tests > 0){
      int n = scanner.nextInt();
      int l = scanner.nextInt();
      int r = scanner.nextInt();
      long[] arr = new long[n];
      for (int i = 0; i < arr.length; i++){
        arr[i] = scanner.nextLong();
      }

      ArrayList<Long> toL = new ArrayList<>();
      ArrayList<Long> toR = new ArrayList<>();
      for (int i = 0; i < r; i++){
        toR.add(arr[i]);
      }
      for (int i = l - 1; i < n; i++){
        toL.add(arr[i]);
      }

      Collections.sort(toL);
      Collections.sort(toR);

      long sumOne = 0;
      for (int i = 0; i < r - l + 1; i++){
        sumOne += toL.get(i);
      }

      long sumTwo = 0;
      for (int i = 0; i < r - l + 1; i++){
        sumTwo += toR.get(i);
      }

      System.out.println(Math.min(sumTwo, sumOne));
      tests--;
    }
  }
}

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class SubsequenceUpdate {

  public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);

    int tests = Integer.parseInt(scanner.nextLine());

    while (tests > 0){

      int n = scanner.nextInt();

      int l = scanner.nextInt();

      int r = scanner.nextInt();

      long[] arr = new long[n];

      for (int i = 0; i < arr.length; i++){

        arr[i] = scanner.nextLong();

      }

      ArrayList<Long> toL = new ArrayList<>();

      ArrayList<Long> toR = new ArrayList<>();

      for (int i = 0; i < r; i++){

        toR.add(arr[i]);

      }

      for (int i = l - 1; i < n; i++){

        toL.add(arr[i]);

      }

      Collections.sort(toL);

      Collections.sort(toR);

      int sumOne = 0;

      for (int i = 0; i < r - l + 1; i++){

        sumOne += toL.get(i);

      }

      int sumTwo = 0;

      for (int i = 0; i < r - l + 1; i++){

        sumTwo += toR.get(i);

      }

      System.out.println(Math.min(sumTwo, sumOne));

      tests--;

    }

  }

}

Answer: 1


#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define ek ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void init()
{

}

void solve() {
    ll n, l, r;
    cin >> n >> l >> r;
    vector<ll> a(n);
    for(ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<ll> b,c,d;
    ll sum=0;
    for(ll i=l-1;i<r;i++) {
        b.push_back(a[i]);
        sum+=a[i];
    }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    for(ll i=0;i<l-1;i++) {
        c.push_back(a[i]);
    }
    for(ll i=r;i<n;i++) {
        d.push_back(a[i]);
    }
    sort(c.begin(), c.end());
    sort(d.begin(), d.end());
    ll ans=sum;
    for(ll i=0;i<min(b.size(), c.size());i++) {
        if(b[i]>c[i]) {
            ans+=(c[i]-b[i]);
        }
    }
    ll ans1=sum;
    for(ll i=0;i<min(b.size(), d.size());i++) {
        if(b[i]>d[i]) {
            ans1+=(d[i]-b[i]);
        }
    }
    cout<<min({sum,ans, ans1})<<endl;

}

int main()
{
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif

    ek;
    ll t = 1;
    cin >> t;
    init();
    while(t--)
    {
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define ek ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void init()
{

}

void solve() {
    ll n, l, r;
    cin >> n >> l >> r;
    vector<ll> a(n);
    for(ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    ll sum=0;
    for(ll i=0;i<r-l+1;i++)
    {
        sum+=a[i];
    }
    cout<<sum<<endl;

}

int main()
{
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif

    ek;
    ll t = 1;
    cin >> t;
    init();
    while(t--)
    {
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define setbitcounts(x)   __builtin_popcountll(x)
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define pll               pair<long,long>
#define vii               vector<int,int>
#define vll               vector<long,long>
#define mii               map<int,int>
#define mci               map<char,int>
#define sti               set<int>
#define stc               set<char>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem0(a)           memset(a,0,sizeof(a))
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppcll             __builtin_popcountll
#define see(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define out(x) cout << x << " ";
#define nl cout << "\n";
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M32=1e9+7;
const int32_t MM32=998244353;
const int N=0;
//ascii value A=65,Z=90,a=97,z=122

ll BS(ll l, ll r, vector<pair<ll,pair<ll,ll>>> &v, ll value){
    ll ans = -1;
    while(l <= r){
        ll mid = (l+r) >> 1L;
        if(v[mid].first < value){
            ans = mid;
            l = mid+1;
        }
        else r = mid-1;
    }
    return ans;
}

int main()
{
IOS
ll t; cin >> t;
while(t--){
ll n, l,r;
cin >> n >> l >> r;

vector<ll>v(n);
rep(i,0,n) cin >> v[i];

sort(all(v));

ll k = r-l+1;

ll ans = 0;

rep(i,0,k){
    ans += v[i];
}

cout << ans << endl;
cout << endl;
}

return 0;
}

/*

*/

#include <bits/stdc++.h>

using namespace std;

#define setbitcounts(x)   __builtin_popcountll(x)

#define ll                long long

#define pb                push_back

#define ppb               pop_back

#define pf                push_front

#define ppf               pop_front

#define all(x)            (x).begin(),(x).end()

#define uniq(v)           (v).erase(unique(all(v)),(v).end())

#define sz(x)             (int)((x).size())

#define fr                first

#define sc                second

#define pii               pair<int,int>

#define pll               pair<long,long>

#define vii               vector<int,int>

#define vll               vector<long,long>

#define mii               map<int,int>

#define mci               map<char,int>

#define sti               set<int>

#define stc               set<char>

#define rep(i,a,b)        for(int i=a;i<b;i++)

#define mem0(a)           memset(a,0,sizeof(a))

#define mem1(a)           memset(a,-1,sizeof(a))

#define mem0(a)           memset(a,0,sizeof(a))

#define ppcll             __builtin_popcountll

#define see(x) cerr << "\n" << (#x) << " is " << (x) << endl

#define out(x) cout << x << " ";

#define nl cout << "\n";

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}

template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;

const int32_t M32=1e9+7;

const int32_t MM32=998244353;

const int N=0;

//ascii value A=65,Z=90,a=97,z=122

ll BS(ll l, ll r, vector<pair<ll,pair<ll,ll>>> &v, ll value){

    ll ans = -1;

    while(l <= r){

        ll mid = (l+r) >> 1L;

        if(v[mid].first < value){

            ans = mid;

            l = mid+1;

        }

        else r = mid-1;

    }

    return ans;

}

int main()

{

IOS

ll t; cin >> t;

while(t--){

ll n, l,r;

cin >> n >> l >> r;

ll k = r-l+1;

vector<ll>v(n);

rep(i,0,n) cin >> v[i];

ll ans1 = 0;

vector<ll>a;

rep(i,0,r) a.push_back(v[i]);

sort(all(a));

rep(i,0,k) ans1 += a[i];

ll ans2 = 0;

vector<ll>b;

rep(i,l-1,n) b.push_back(v[i]);

sort(all(b));

rep(i,0,k) ans2 += b[i];

ll ans = min(ans1,ans2);

cout << ans << endl;

cout << endl;

}

return 0;

}

Answer: 2