#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> ma;
int n,m;
bool cal(int x)
{
if(ma[x]>0)
{
	ma[x]--;
	n--;
	return 1;
}
if(x==1)
	return 0;
return cal(x/2)&&cal(x/2+x%2);
}
void solve()
{

	cin>>n>>m;
	ma.clear();
	for(int i=1;i<=n;i++)
	{
		int temp;cin>>temp;
		ma[temp]++;
	}
	for(int i=1;i<=m;i++)
	{
		int temp;cin>>temp;
		if(cal(temp)==0)
		{
			cout<<"NO\n";
			for(int j=i+1;j<=m;j++)
				cin>>temp;
			return ;
		}
	}
	if(n==0)
	cout<<"YES\n";
else cout<<"NO\n";
	return;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	while(T--)
	solve();
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int,int> ma;
int n,m;
bool cal(int x)
{
if(ma[x]>0)
{
	ma[x]--;
	n--;
	return 1;
}
if(x==1)
	return 0;
return cal(x/2)&&cal(x/2+x%2);
}
void solve()
{

	cin>>n>>m;
	ma.clear();
	for(int i=1;i<=n;i++)
	{
		int temp;cin>>temp;
		ma[temp]++;
	}
	for(int i=1;i<=m;i++)
	{
		int temp;cin>>temp;
		if(cal(temp)==0)
		{
			cout<<"NO\n";
			return ;
		}
	}
	if(n==0)
	cout<<"YES\n";
else cout<<"NO\n";
	return;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T;
	cin>>T;
	while(T--)
	solve();
}

Answer: 1


#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int a[300005],b[300005];
map<int,int>gs;
multiset<pii>s;
int dfs(){
	while(!s.empty()){
		pii o;
		while(s.size()>1){
			auto e=--s.end();
			o=*e;
			e--;
			pii t=*e;
			if(o.x==t.x)
				o.y+=t.y,s.erase(--s.end()),s.erase(--s.end()),s.insert(o);
			else
				break;
		}
		o=*--s.end();
		s.erase(--s.end());
		if(gs[o.x]>=o.y)
			gs[o.x]-=o.y;
		else{
			s.insert({o.x/2,o.y-gs[o.x]});
			s.insert({o.x-o.x/2,o.y-gs[o.x]});
			gs[o.x]=0;
			if(o.x==1)
				break;
		}
	}
	if(s.size()==0)
		return 1;
	else
		return 0;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T;
	cin>>T;
	while(T--){
		s.clear();
		int n,m,sum=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			cin>>a[i],gs[a[i]]++,sum+=a[i];
		for(int i=1;i<=m;i++){
			cin>>b[i];
			sum-=b[i];
			s.insert({b[i],1});
		}
		if(dfs()&&sum==0)
			cout<<"Yes\n";
		else
			cout<<"No\n";
		for(int i=1;i<=n;i++)
			gs[a[i]]=0;
	}
	return 0;
}
/*

*/

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int a[300005],b[300005];
map<int,int>gs;
multiset<pii>s;
int dfs(){
	while(!s.empty()){
		pii o=*s.begin();
		while(s.size()>1){
			auto e=s.begin();
			o=*e;
			e++;
			pii t=*e;
			if(o.x==t.x)
				o.y+=t.y,s.erase(s.begin()),s.erase(s.begin()),s.insert(o);
			else
				break;
		}
		o=*s.begin();
		s.erase(s.begin());
		if(gs[o.x]>=o.y)
			gs[o.x]-=o.y;
		else{
			if(o.y>gs[o.x])
			s.insert({o.x/2,o.y-gs[o.x]});
			s.insert({o.x-o.x/2,o.y-max(0ll,gs[o.x]-o.y)});
			gs[o.x]=0;
			if(o.x==1)
				break;
			if(o.x==0)
				break;
		}
	}
	if(s.size()==0)
		return 1;
	else
		return 0;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T;
	cin>>T;
	while(T--){
		s.clear();
		int n,m,sum=0;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			cin>>a[i],gs[a[i]]++,sum+=a[i];
		for(int i=1;i<=m;i++){
			cin>>b[i];
			sum-=b[i];
			s.insert({b[i],1});
		}
		if(dfs()&&sum==0)
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
	return 0;
}

Answer: 1


/*
*   author: bobbyCodes
*/

#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/treepolicy.hpp>
//#define pbds tree<ll , null_type, less , rb_tree_tag, tree_order_static_node_update>
#define ll long long
#define pb push_back
#define IM INT_MAX
#define IN INT_MIN
#define fo for(int i=0;i<n;i++){}
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
/* -------------------------------------------------------------------------------------------------------------
//ll gcd(ll a,ll b){for(ll i = min(a,b);i >=1;i--){if(!(a%i) && !(b%i)){return i;}}return 1;}
ll seiveOfErytosthesis(ll k,ll n = 1e6){vector<bool> v(n+1,true);v[0]=v[1]=false;for(ll i =2; i*i<= n; i++){for(ll j =2*i; j <=n ; j+=i){if(v[j]){v[j]=false;}}}ll num=0;for(ll i=k; i<=1e6;i++){if(v[i]){num= i;break;}}return num;}
-------------------------------------------------------------------------------------------------------------
*/
//using namespace __gnu_pbds

void init_code(){
    fast_io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
void solve(){
   int n;
   cin>>n;

   vector<ll>v(n);

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

   int now=n;

   long long ans= -1e18;

   for(int i=0; i<n ;i ++){
      long long sum=0;
      for(int j=0; j< now; j++ ){
         sum+=v[j];
      }
      if(i==0){
         ans=max(ans,sum);
      }else{
         ans=max(ans, max(sum,(-sum)));
      }
      for(int j=0; j<now-1; j++){
         v[j]=v[j+1] - v[j];
      }
      now--;
   }
   cout<<ans<<endl;
}
int main() {
   init_code();
   int t = 1; cin >> t;
   while(t--){
         solve();
   }
   return 0;
}

/*
*   author: bobbyCodes
*/

#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/treepolicy.hpp>
//#define pbds tree<ll , null_type, less , rb_tree_tag, tree_order_static_node_update>
#define ll long long
#define pb push_back
#define IM INT_MAX
#define IN INT_MIN
#define fo for(int i=0;i<n;i++){}
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
/* -------------------------------------------------------------------------------------------------------------
//ll gcd(ll a,ll b){for(ll i = min(a,b);i >=1;i--){if(!(a%i) && !(b%i)){return i;}}return 1;}
ll seiveOfErytosthesis(ll k,ll n = 1e6){vector<bool> v(n+1,true);v[0]=v[1]=false;for(ll i =2; i*i<= n; i++){for(ll j =2*i; j <=n ; j+=i){if(v[j]){v[j]=false;}}}ll num=0;for(ll i=k; i<=1e6;i++){if(v[i]){num= i;break;}}return num;}
-------------------------------------------------------------------------------------------------------------
*/
//using namespace __gnu_pbds

void init_code(){
    fast_io;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
void solve(){
   int n;
   cin>>n;

   vector<int>v(n);

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

   int now=n;

   long long ans= -1e18;

   for(int i=0; i<n ;i ++){
      long long sum=0;
      for(int j=0; j< now; j++ ){
         sum+=v[j];
      }
      if(i==0){
         ans=max(ans,sum);
      }else{
         ans=max(ans, max(sum,(-sum)));
      }
      for(int j=0; j<now-1; j++){
         v[j]=v[j+1] - v[j];
      }
      now--;
   }
   cout<<ans<<endl;
}
int main() {
   init_code();
   int t = 1; cin >> t;
   while(t--){
         solve();
   }
   return 0;
}

Answer: 1


#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
using namespace std;
#define ll long long
#define re(i,b) for(ll i=0;i<b;i++)
#define rre(i,b) for(ll i=b;i>=0;i--)
#define dd long double
#define all(x) x.begin(), x.end()
#define rall(a) (a).rbegin(), (a).rend()
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define lire(v) for (auto &u : v) cin >> u;
#define aff(v) for (auto u : v) cout << u << " "; cout << endl;
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define mpcl map<char, ll>
#define vdd vector<dd>
#define vbo vector<bool>
#define mpll map<ll, ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vss vector<string>
#define vcc vector<char>
#define scc set<char>
#define sss set<string>
// sort(a.begin(), a.end(), [](int x, int y) {
//       return x % 2 < y % 2;
//     });
//fixed<<setprecision(6)
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;
    cin >> t;
    re(w,t){
       ll n;cin>>n;
       vll a(n);
       ll s=LLONG_MIN;

       re(i,n){
           ll s2=0;
           re(j,n-i){
               s2+=a[j];
           }
           if(i==0)s=max(s,s2);
           else s=max(s,max(s2,-(s2)));
           re(j,n-i-1)a[j]=a[j+1]-a[j];
       }
       cout<<s<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
using namespace std;
#define ll long long
#define re(i,b) for(ll i=0;i<b;i++)
#define rre(i,b) for(ll i=b;i>=0;i--)
#define dd long double
#define all(x) x.begin(), x.end()
#define rall(a) (a).rbegin(), (a).rend()
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define lire(v) for (auto &u : v) cin >> u;
#define aff(v) for (auto u : v) cout << u << " "; cout << endl;
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define mpcl map<char, ll>
#define vdd vector<dd>
#define vbo vector<bool>
#define mpll map<ll, ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vss vector<string>
#define vcc vector<char>
#define scc set<char>
#define sss set<string>
// sort(a.begin(), a.end(), [](int x, int y) {
//       return x % 2 < y % 2;
//     });
//fixed<<setprecision(6)
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t;
    cin >> t;
    re(w,t){
       ll n;cin>>n;
       vll a(n);
       ll s=LLONG_MIN;
       re(i,n){
           cin>>a[i];
       }
       re(i,n){
           ll s2=0;
           re(j,n-i){
               s2+=a[j];
           }
           if(i==0)s=max(s,s2);
           else s=max(s,max(s2,-(s2)));
           re(j,n-i-1)a[j]=a[j+1]-a[j];
       }
       cout<<s<<endl;
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define pll pair <ll, ll>
#define pii pair <int, int>
#define pb push_back
#define rnd srand(static_cast<unsigned int>(time(0)))
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
const int inf = 2e9;
void solve(){
    int n;
    ll mx = 0, sum = 0;
    cin >> n;
    vector <ll> a(n);
    for (int i = 0; i < n; i ++){
        cin >> a[i];
        sum += a[i];
    }
    if (n > 1) mx = max(mx, abs(sum));
    else mx = max(mx, sum);
    while((int)a.size() != 1){
        for (int i = 0; i < (int)a.size() - 1; i ++){
            a[i] *= (-1LL);
            a[i] += a[i + 1];
        }
        a.pop_back();
        sum = 0;
        for (ll i : a)
            sum += i;
        mx = max(mx, abs(sum));
    }
    cout << mx << endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t --)
        solve();
}

#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define pll pair <ll, ll>
#define pii pair <int, int>
#define pb push_back
#define rnd srand(static_cast<unsigned int>(time(0)))
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
const int inf = 2e9;
void solve(){
    int n;
    ll mx = -2e18, sum = 0;
    cin >> n;
    vector <ll> a(n);
    for (int i = 0; i < n; i ++){
        cin >> a[i];
        sum += a[i];
    }
    mx = max(mx, sum);
    while((int)a.size() != 1){
        for (int i = 0; i < (int)a.size() - 1; i ++){
            a[i] *= (-1LL);
            a[i] += a[i + 1];
        }
        a.pop_back();
        sum = 0;
        for (ll i : a)
            sum += i;
        if (n > 1) mx = max(mx, abs(sum));
        else mx = max(mx, sum);
    }
    cout << mx << endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t --)
        solve();
}

Answer: 2


#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;
template<class T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

#define dbg(x) cerr << #x << " = " << x << endl;
#define int long long
#define double long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(),a.rend()
#define endl "\n"
const int mod = 1e9+ 7;
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int lcm(int a,int b){
    return a/__gcd(a,b)*b;
}

const int maxN = 1e3 + 5, inf = 1e18;

int sum(vector<int>&a){
    int res =0 ;
    for(int x:a)res += x;
    return res;
}
void solve(){
    // so importa o primeiro e o ultimo

    int n, ans = 0;
    cin >> n;

    vector<int>a(n);
    for(int &i:a){
        cin >> i;
        ans += i;
    }

    while(a.size() > 1){
        int x = a.back() - a.front();

        ans = max(ans, x);
        // cout << "sz: " << a.size() << endl;
        if(x < 0){
            reverse(all(a));
        }

        vector<int>b(a.size() - 1);
        for(int i = 0; i < a.size()-1; i++)b[i] = a[i+1] - a[i];

        a = b;
    }
    ans = max(ans, a[0]);
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;

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

    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;
template<class T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, std::less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

#define dbg(x) cerr << #x << " = " << x << endl;
#define int long long
#define double long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(),a.rend()
#define endl "\n"
const int mod = 1e9+ 7;
const int dx[] = { 1, 0, -1, 0 };
const int dy[] = { 0, 1, 0, -1 };

int lcm(int a,int b){
    return a/__gcd(a,b)*b;
}

const int maxN = 1e3 + 5, inf = 1e18;

int sum(vector<int>&a){
    int res =0 ;
    for(int x:a)res += x;
    return res;
}
void solve(){
    // so importa o primeiro e o ultimo

    int n, ans = 0;
    cin >> n;

    vector<int>a(n);
    for(int &i:a){
        cin >> i;
        ans += i;
    }

    while(a.size() > 1){
        // for(int x:a)cout << x << " ";
        // cout << endl;
        int x = a.back() - a.front();

        ans = max(ans, sum(a));
        // cout << "sz: " << a.size() << " x: " << x << " ans: " << ans << endl;
        if(x < 0){
            reverse(all(a));
        }

        vector<int>b(a.size() - 1);
        for(int i = 0; i < a.size()-1; i++)b[i] = a[i+1] - a[i];

        a = b;
    }
    ans = max(ans, a[0]);
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;

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

    return 0;
}

Answer: 2


#include <bits/stdc++.h>

#define endl '\n'
typedef long long ll;
using namespace std;

template <typename T>T gcd(T a, T b) {if (b == 0) return a; return gcd(b, a % b);}
template <typename T> T lcm(T a, T b) {return (a * b) / gcd(a, b);}
template <typename K> void print_vec(const vector<K>& vec) {for(size_t i = 0; i < vec.size(); ++i) {cout << vec[i];if(i != vec.size() - 1) {cout << " ";}}cout << endl;}

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

    vector<int> a(n);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ans += a[i];
    }

    ans = abs(ans);

    while (n > 1) {
        ll x = 0;
        for (int i = 0; i < n - 1; i++) {
            a[i] = a[i + 1] - a[i];
            x += a[i];
        }

        ans = max(ans, abs(x));
        n--;
    }

    cout << ans << endl;
}

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

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

    return 0;
}

#include <bits/stdc++.h>

#define endl '\n'
typedef long long ll;
using namespace std;

template <typename T>T gcd(T a, T b) {if (b == 0) return a; return gcd(b, a % b);}
template <typename T> T lcm(T a, T b) {return (a * b) / gcd(a, b);}
template <typename K> void print_vec(const vector<K>& vec) {for(size_t i = 0; i < vec.size(); ++i) {cout << vec[i];if(i != vec.size() - 1) {cout << " ";}}cout << endl;}

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

    vector<ll> a(n);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ans += a[i];
    }

    while (n > 1) {
        ll x = 0;
        for (int i = 0; i < n - 1; i++) {
            a[i] = a[i + 1] - a[i];
            x += a[i];
        }

        ans = max(ans, abs(x));
        n--;
    }

    cout << ans << endl;
}

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

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

    return 0;
}

Answer: 2


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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> v;
        for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            v.push_back(a);
        }
        if(n == 1){
            cout << v[0] << endl;
            continue;
        }
        ll ans = accumulate(v.begin(), v.end(), 0LL);
        vector<int> u;
        while(n > 1){
            for(int i = 0; i < n - 1; i++)u.push_back(v[i + 1] - v[i]);
            ll sum = accumulate(u.begin(), u.end(), 0LL);
            for(int i = 0; i < n - 1; i++)v[i] = u[i];
            u.clear();
            ans = max(ans, abs(sum));
            n--;
        }
        cout << ans << endl;
    }
}

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ll> v;
        for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            v.push_back(a);
        }
        if(n == 1){
            cout << v[0] << endl;
            continue;
        }
        ll ans = accumulate(v.begin(), v.end(), 0LL);
        vector<ll> u;
        while(n > 1){
            for(int i = 0; i < n - 1; i++)u.push_back(v[i + 1] - v[i]);
            ll sum = accumulate(u.begin(), u.end(), 0LL);
            for(int i = 0; i < n - 1; i++)v[i] = u[i];
            u.clear();
            ans = max(ans, abs(sum));
            n--;
        }
        cout << ans << endl;
    }
}

Answer: 2


#include <bits/stdc++.h>
#define NMAX 55
#define pb push_back

using namespace std;

int v[NMAX];

int main() {
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        long long maxx = LONG_LONG_MIN;
        cin >> n;
        long long s = 0;
        for (int i = 1; i <= n; i++) {
            cin >> v[i];
            s += v[i];
        }
        maxx = max(maxx, s);
        int p = n;
        while(p > 1) {
            long long s1 = 0, s2 = 0;
            for (int i = 1; i < p; i++) {
                v[i] = v[i + 1] - v[i];
                s1 += v[i];
                s2 += -(v[i]);
            }
            p--;
            maxx = max(maxx, max(s1, s2));
        }
        if (n > 1)
            cout << maxx << '\n';
        else cout << v[1] << '\n';
    }
    return 0;
}

#include <bits/stdc++.h>
#define NMAX 55
#define pb push_back

using namespace std;

long long v[NMAX];

int main() {
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        long long maxx = LONG_LONG_MIN;
        cin >> n;
        long long s = 0;
        for (int i = 1; i <= n; i++) {
            cin >> v[i];
            s += v[i];
        }
        maxx = max(maxx, s);
        int p = n;
        while(p > 1) {
            long long s1 = 0, s2 = 0;
            for (int i = 1; i < p; i++) {
                v[i] = v[i + 1] - v[i];
                s1 += v[i];
                s2 += -(v[i]);
            }
            p--;
            maxx = max(maxx, max(s1, s2));
        }
        if (n > 1)
            cout << maxx << '\n';
        else cout << v[1] << '\n';
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define umi unordered_map<int,int>
#define usi unordered_set<int>
#define all(x) x.begin(),x.end()
#define fr(i, n) for (long long i = 0; i < (n); i++)
using namespace std;

const int mod = 1e9+7;

int sol(vector<int> v) {
    int n = v.size();
    if (n == 1) {
        return v[0];
    }

    vi ki, kir;
    for (int i = 1; i < n; i++) {
        ki.pb(v[i] - v[i - 1]);
    }

    reverse(all(v));

    for (int i = 1; i < n; i++) {
        kir.pb(v[i] - v[i - 1]);
    }

    if (ki.empty() || kir.empty()) return 0; // Avoid infinite recursion

    return max({sol(ki), sol(kir)});
}

void solve() {
    int n;
    cin >> n;
    vi v(n);
    fr(i, n) {
        cin >> v[i];
    }

    int sm = accumulate(all(v), 0LL);
    int mi = sol(v);

    cout << max(mi, sm) << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
    }
}

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define vi vector<ll>
#define vvi vector<vi>
#define umi unordered_map<ll,ll>
#define usi unordered_set<ll>
#define all(x) x.begin(),x.end()
#define fr(i, n) for (long long i = 0; i < (n); i++)
using namespace std;

const ll mod = 1e9+7;

vector<ll>sol(vector<ll> v) {
  ll n=v.size();
  vi ki;
  for(ll i=1;i<n;i++){
    ki.pb(v[i]-v[i-1]);
  }
  ll sm=accumulate(all(ki),0LL);
  if(sm<0){
    reverse(all(v));
    ki.clear();
    for(ll i=1;i<n;i++){
       ki.pb(v[i]-v[i-1]);
    }
  }
  return ki;
}

void solve() {
    ll n;
    cin >> n;
    vi v(n);
    fr(i, n) {
        cin >> v[i];
    }ll mx=INT_MIN;ll si=accumulate(all(v),0LL);
 while(!v.empty()){
    ll sm=accumulate(all(v),0LL);
    v=sol(v);
    mx=max(mx,sm);
}
cout<<max(mx,si)<<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
    }
}

Answer: 2


#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define il inline
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ld long double
#define db double
#define gc getchar();
#define pc(x) putchar(x)
#define O(x) cout<<x<<'\n';
#define adde(x,y) emplace_back(make_pair(x,y))
#define int long long

#define pbset(typ) tree< typ ,null_type,std::less< typ >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define pbmap(typ1,typ2) tree< typ1 , typ2 ,std::less< typ1 >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>

namespace constant_warrior{
	template<typename T> inline void fr(T& num){
		num=0;short sign=1;char ch=std::getchar();
		while(ch<'0'||ch>'9'){
			if(ch=='-')sign=-1;
			ch=std::getchar();
		}
		while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
		num=num*sign;
	}
	template<typename T>inline void fw(T x){
		if(x<0)std::putchar('-'),x=-x;
		if(x>9)fw(x/10);
		std::putchar(x%10+'0');
	}
	template<typename T>inline const T& maxf(const T& a,const T& b){
		if(a>b)return a;
		return b;
	}
	template<typename T>inline const T& minf(const T& a,const T& b){
		if(a>b)return b;
		return a;
	}
	template<typename T>inline void swapf(T& a,T& b){
		a^=b^=a^=b;
	}
}
using namespace constant_warrior;
const int N=64;
int n,m,a[2][N];

void solve(){
	int res=0;
	fr(n);
	for(int i=1;i<=n;i++){
		fr(a[0][i]);
		res+=a[0][i];
	}
	for(bool flag=1;n>1;n--,flag^=1){
		int tmp=0;
		for(int i=1;i<n;i++){
			tmp+=(a[flag][i]=a[flag^1][i+1]-a[flag^1][i]);
		}
		res=max(res,abs(tmp));
	}
	fw(res),pc('\n');

}
_Atomic_word main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
	cin>>T;
	while(T-->0)solve();
}
/*
暴力出奇迹，卡常能AC。
Violent makes miracle,pursuing better constant can AC.
多测不清空，OI见祖宗。
multitesting without clearing,oier meets the LCA.
十年OI一场空，不开LL见祖宗。
Ten years of OI just AFO,no #define int long long sees the LCA.
似是神犇成才处，实为蒟蒻黄泉路。
It is likely to be the Au medal for the big old,but in fact it is the Si medal for me.
黄题有恨无正解，码力不若小学生。
A yellow problem I can't AC,codeforces is not as NB as HNO3(Dilute nitric acid).
今生无奈入OI，来世不做信竞人。
This life I am a Siliy Being in oi,next life I won't f**k the sh*t of infomatics.
你%你赛觉得是我的锅那就是我的锅，
因为%你赛出的题，就像是一个毒瘤晚期患者出的题，
他都已经这样了，你为什么不能顺从他呢？
你总要给人最后一段时间一个好的回忆吧，最后的时光里。
*/

#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define il inline
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ld long double
#define db double
#define gc getchar();
#define pc(x) putchar(x)
#define O(x) cout<<x<<'\n';
#define adde(x,y) emplace_back(make_pair(x,y))

#define pbset(typ) tree< typ ,null_type,std::less< typ >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define pbmap(typ1,typ2) tree< typ1 , typ2 ,std::less< typ1 >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>

namespace constant_warrior{
	template<typename T> inline void fr(T& num){
		num=0;short sign=1;char ch=std::getchar();
		while(ch<'0'||ch>'9'){
			if(ch=='-')sign=-1;
			ch=std::getchar();
		}
		while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
		num=num*sign;
	}
	template<typename T>inline void fw(T x){
		if(x<0)std::putchar('-'),x=-x;
		if(x>9)fw(x/10);
		std::putchar(x%10+'0');
	}
	template<typename T>inline const T& maxf(const T& a,const T& b){
		if(a>b)return a;
		return b;
	}
	template<typename T>inline const T& minf(const T& a,const T& b){
		if(a>b)return b;
		return a;
	}
	template<typename T>inline void swapf(T& a,T& b){
		a^=b^=a^=b;
	}
}
using namespace constant_warrior;
const int N=64;
int n,m,a[2][N];

void solve(){
	int res=0;
	fr(n);
	for(int i=1;i<=n;i++){
		fr(a[0][i]);
		res+=a[0][i];
	}
	for(bool flag=1;n>1;n--,flag^=1){
		int tmp=0;
		for(int i=1;i<n;i++){
			tmp+=(a[flag][i]=a[flag^1][i+1]-a[flag^1][i]);
		}
		res=max(res,abs(tmp));
	}
	fw(res),pc('\n');

}
int main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
	cin>>T;
	while(T-->0)solve();
}
/*
暴力出奇迹，卡常能AC。
Violent makes miracle,pursuing better constant can AC.
多测不清空，OI见祖宗。
multitesting without clearing,oier meets the LCA.
十年OI一场空，不开LL见祖宗。
Ten years of OI just AFO,no #define int long long sees the LCA.
似是神犇成才处，实为蒟蒻黄泉路。
It is likely to be the Au medal for the big old,but in fact it is the Si medal for me.
黄题有恨无正解，码力不若小学生。
A yellow problem I can't AC,codeforces is not as NB as HNO3(Dilute nitric acid).
今生无奈入OI，来世不做信竞人。
This life I am a Siliy Being in oi,next life I won't f**k the sh*t of infomatics.
你%你赛觉得是我的锅那就是我的锅，
因为%你赛出的题，就像是一个毒瘤晚期患者出的题，
他都已经这样了，你为什么不能顺从他呢？
你总要给人最后一段时间一个好的回忆吧，最后的时光里。
*/

Answer: 1


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

int main(){
    int cases;
    scanf("%d",&cases);
    while(cases--){
        int n;
        cin >> n;
        int sum=0;
        vector<int> arr;
        int num;
        for(int i=0;i<n;i++){
            cin >> num;
            arr.push_back(num);
            sum+=arr[i];

        }
        vector<int> vec;
        vec.push_back(sum);
        while(arr.size()>1){
            int n=arr.size();
            if(arr[0] < arr[n-1])
            {
                vec.push_back(arr[n-1]-arr[0]);
                for(int i=0;i<n-1;i++)
                {
                    arr[i]=arr[i+1]-arr[i];
                }
                arr.pop_back();
            }
            else{
                vec.push_back(arr[0]-arr[n-1]);
                for(int i=0;i<n-1;i++)
                {
                    arr[i]=arr[i]-arr[i+1];
                }
                arr.pop_back();
            }
        }
        stable_sort(vec.begin(),vec.end());
        int m=vec.size();
        cout << vec[m-1] << endl;

    }
}

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

int main(){
    long long int cases;
    scanf("%lld",&cases);
    while(cases--){
        long long int n;
        cin >> n;
        long long int sum=0;
        vector<long long int> arr;
        long long int num;
        for(long long int i=0;i<n;i++){
            cin >> num;
            arr.push_back(num);
            sum+=arr[i];

        }
        vector<long long int> vec;
        vec.push_back(sum);
        while(arr.size()>1){
            long long int n=arr.size();
            if(arr[0] < arr[n-1])
            {
                vec.push_back(arr[n-1]-arr[0]);
                for(long long int i=0;i<n-1;i++)
                {
                    arr[i]=arr[i+1]-arr[i];
                }
                arr.pop_back();
            }
            else{
                vec.push_back(arr[0]-arr[n-1]);
                for(long long int i=0;i<n-1;i++)
                {
                    arr[i]=arr[i]-arr[i+1];
                }
                arr.pop_back();
            }
        }
        stable_sort(vec.begin(),vec.end());
        long long int m=vec.size();
        cout << vec[m-1] << endl;

    }
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t!=0){
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++) cin>>arr[i];

        long long maxi=LLONG_MIN;
        int now=n;
        for(int i=0;i<n;i++){
            long long sum=0;
            for(int j=0;j<now;j++){
                sum+=arr[j];
            }
            if(i==0){
                maxi=max(maxi,sum);
            }
            else{
                maxi=max(maxi,max(sum,(-sum)));
            }

            for(int j=0;j<now-1;j++){
                arr[j]=arr[j+1]-arr[j];
            }
            now--;
        }
        cout<<maxi<<endl;
        t--;
    }
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t!=0){
        int n;
        cin>>n;
        long long arr[n];
        for(int i=0;i<n;i++) cin>>arr[i];

        long long maxi=LLONG_MIN;
        int now=n;
        for(int i=0;i<n;i++){
            long long sum=0;
            for(int j=0;j<now;j++){
                sum+=arr[j];
            }
            if(i==0){
                maxi=max(maxi,sum);
            }
            else{
                maxi=max(maxi,max(sum,(-sum)));
            }

            for(int j=0;j<now-1;j++){
                arr[j]=arr[j+1]-arr[j];
            }
            now--;
        }
        cout<<maxi<<endl;
        t--;
    }
}

Answer: 2


#include <stdio.h>
#include <math.h>
int main(){
    int n;
    scanf("%d\n",&n);
    long long int res[n];
    for (int i=0;i<n;i++){
        int k;
        long long int max=0;
        scanf("%d\n",&k);
        int arr[k];
        for (int j=0;j<k;j++){
            scanf("%d",&arr[j]);
            max=max+arr[j];
        }
        for (int y=1;y<k;y++){
            long long int temp=0,value=1;
            if (y%2==1){
                for (int u=0;u<y;u++){
                    temp=temp+(value)*((int)pow(-1,u))*(arr[k-1-u]-arr[u]);
                    value=(value*(y-1-u))/(u+1);
                }
            }
            else {
                for (int u=0;u<y;u++){
                    temp=temp+(value)*((int)pow(-1,u))*(arr[k-1-u]+arr[u]);
                    value=(value*(y-1-u))/(u+1);
                }
            }
            // printf("%d %d\n",y,temp);
            if (temp>max){
                max=temp;
            }
            else if (-1*temp>max){
                max=-1*temp;
            }
        }
        res[i]=max;
    }
    for (int b=0;b<n;b++){
        printf("%lld\n",res[b]);
    }
}

#include <stdio.h>
#include <math.h>
int main(){
    int n;
    scanf("%d\n",&n);
    long long int res[n];
    for (int i=0;i<n;i++){
        int k;
        long long int max=0;
        scanf("%d\n",&k);
        int arr[k];
        for (int j=0;j<k;j++){
            scanf("%d",&arr[j]);
            max=max+arr[j];
        }
        for (int y=1;y<k;y++){
            long long int temp=0,value=1;
            if (y%2==1){
                for (int u=0;u<y;u++){
                    temp=temp+(value)*((int)pow(-1,u))*(arr[k-1-u]-arr[u]);
                    value=(value*(y-1-u))/(u+1);
                }
            }
            else {
                for (int u=0;u<y;u++){
                    temp=temp+(value)*((int)pow(-1,u))*(arr[k-1-u]+arr[u]);
                    value=(value*(y-1-u))/(u+1);
                }
            }
            // printf("%d %d\n",y,temp);
            if (temp>max){
                max=temp;
            }
            if (-1*temp>max){
                max=-1*temp;
            }
        }
        res[i]=max;
    }
    for (int b=0;b<n;b++){
        printf("%lld\n",res[b]);
    }
}

Answer: 2


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

#define ll long long
#define int ll

const int sz = 2e5 + 5;

void solve(){
    int n, sum = 0, ans = -1e9; cin >> n;
    vector<int>a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i]; sum += a[i];
    }
    ans = max(ans, sum); sum = 0;
    while(a.size() > 1){
        reverse(a.begin(), a.end());
        vector<int>b(a.size() - 1);
        for(int i = 1; i < a.size(); i++){
            b[i - 1] = a[i] - a[i - 1];
            sum += b[i - 1];
        }
        ans  = max(ans, abs(sum)); sum = 0;
        swap(a, b);
    }
    cout << ans << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while(t--) solve();
}

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

#define ll long long
#define int ll

const int sz = 2e5 + 5;

void solve(){
    int n, sum = 0, ans = -1e9; cin >> n;
    vector<int>a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i]; sum += a[i];
    }
    ans = max(ans, sum); sum = 0;
    while(a.size() > 1){
        reverse(a.begin(), a.end());
        vector<int>b(a.size() - 1);
        for(int i = 1; i < a.size(); i++){
            b[i - 1] = a[i] - a[i - 1];
            sum += b[i - 1];
        }
        ans  = max(ans, sum); sum = 0;
        swap(a, b);
    }
    cout << ans << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while(t--) solve();
}

Answer: 1


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
      long long n;
      cin>>n;
      vector<long long> a(n);
      long long sum=0;
      for(auto&i:a)
      {
        cin>>i;
        sum+=i;
      }
      vector<long long> store;
      while(n>1)
      {
        for(int i=0;i<n-1;i++)
        {
          store.push_back(a[i+1]-a[i]);
        }

        long long sum2=0;
        for(auto&i:store)sum2+=i;

        sum=max(sum,abs(sum2));

        for(int i=0;i<store.size();i++)
        {
          a[i]=store[i];
        }
        a.pop_back();
        n--;
        store.clear();
      }
      cout<<sum<<endl;
    }
}

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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
      long long n;
      cin>>n;
      vector<long long> a(n);
      long long sum=0;
      for(auto&i:a)
      {
        cin>>i;
        sum+=i;
      }
      vector<int> store;
      while(n>1)
      {
        for(int i=0;i<n-1;i++)
        {
          store.push_back(a[i+1]-a[i]);
        }

        long long sum2=0;
        for(auto&i:store)sum2+=i;

        sum=max(sum,abs(sum2));

        for(int i=0;i<store.size();i++)
        {
          a[i]=store[i];
        }
        a.pop_back();
        n--;
        store.clear();
      }
      cout<<sum<<endl;
    }
}

Answer: 1


//Coded by:-suvampaul
#include<iostream>
#include<algorithm>
#include<vector>
#include<sstream>
#include<stack>
#include<deque>
#include<queue>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<numeric>
#include<string>
#include<iterator>
#include<limits>
using namespace std;

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define pii pair<int,int>
#define vpii vector<pii>
#define rep(i,n) for(int i=0; i<n; i++)
#define All(x,v) for(auto x:v)
#define arrin(a,n) ll a[n]; rep(i,n) cin>>a[i]
#define vi vector<int>
#define vll vector<ll>
#define nl "\n"
#define print(arr) All(x,arr) cout<<x<<" "; cout<<nl
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define ing(m,ad) rep(i,m){ int u,v; cin>>u>>v; u--;v--; ad[u].pb(v); ad[v].pb(u);}
#define vdsort(v) sort(v.begin(),v.end(),greater<int>())
#define F first
#define S second
#define mod 1'000'000'007

const int MAXN = 100'005;
int spf[MAXN];
ll fac1[MAXN], fac2[MAXN], inv[MAXN];
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <typename C, typename T = std::decay_t<decltype(*begin(std::declval<C>()))>, typename std::enable_if<!std::is_same<C, std::string>::value>::type* = nullptr>
std::ostream& operator<<(std::ostream& os, const C& container)
{
    bool first = true;
    std::stringstream ss;
    ss << '[';
    for (const auto& x : container)
    {
        if (!first)
        {
            ss << ", ";
        }
        first = false;
        ss << x;
    }
    ss << ']';
    return os << ss.str();
}
template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p)
{
    os << '{' << p.first << ", " << p.second << '}';
    return os;
}
vector<string> vec_splitter(string S)
{
    S += ',';
    vector<string> res;
    while (!S.empty())
    {
        res.push_back(S.substr(0, S.find(',')));
        S = S.substr(S.find(',') + 1);
    }
    return res;
}

template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T)
{
    if (idx > 0)
        cerr << ", ";
    else
        cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss;
    ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef DEBUG
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

bool isPerfectSquare(long double x)
{
    if (x >= 0) {
        long long sr = sqrt(x);
        return (sr * sr == x);
    }
    return false;
}

bool isPowerof2(int n, int) {
    return (n && !(n & n - 1));
}

string decimalToBin(ll N)
{
    string B_Number = "";
    int c = 0;
    while (N != 0) {
        if (N % 2 == 0) {
            B_Number = "0" + B_Number;
        }
        else B_Number = "1" + B_Number;
        N = N / 2;
        c++;
    }

    for (int i = c + 1; i <= 30; i++) {
        B_Number = "0" + B_Number;
    }

    return B_Number;
}

int binaryToDecimal(ll n)
{
    ll num = n;
    int dec_value = 0;
    int base = 1;
    ll temp = num;

    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;

        dec_value += last_digit * base;

        base = base * 2;
    }

    return dec_value;
}

ll min(ll a, ll b) {
    return a < b ? a : b;
}
ll max(ll a, ll b) {
    return a > b ? a : b;
}
ll power(ll a, ll b, ll Mod) {
    if (b == 0) {
        return 1;
    }
    ll ans = power(a, b / 2, Mod);
    ans *= ans;
    ans %= Mod;
    if (b % 2) {
        ans *= a;
    }
    return ans % Mod;
}

ll getMin(vector<ll> arr, int n)
{
    ll res = arr[0];
    for (int i = 1; i < n; i++)
        res = min(res, arr[i]);
    return res;
}
ll getMax(vector<ll> arr, int n)
{
    ll res = arr[0];
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    return res;
}

string adder(string s1, string s2) {
    vector<int> num;
    int len1 = s1.length();
    int len2 = s2.length();
    int len = max(len1, len2);
    for (int i = len1 + 1; i <= len; i++) s1 = "0" + s1;
    for (int i = len2 + 1; i <= len; i++) s2 = "0" + s2;
    for (int i = 0; i < len + 1; i++) num.push_back(0);
    int c = num.size();
    for (int i = 0; i < len; i++) {
        int x1 = s1[len - 1 - i] - '0';
        int x2 = s2[len - 1 - i] - '0';
        num[c - 1 - i] += (x1 + x2);
        num[c - 1 - i - 1] += num[c - 1 - i] / 10;
        num[c - 1 - i] = num[c - 1 - i] % 10;
    }
    string ans = "";
    int idx = 0;
    for (int i = 0; i < c; i++) {
        if (num[i] != 0) {
            idx = i; break;
        }
    }
    for (int i = idx; i < c; i++) {
        ans += to_string(num[i]);
    }
    return ans;
}

ll accurateFloor(ll a, ll b) {
    ll val = a / b;
    while (val * b > a)
        val--;
    return val;
}

vi formSum(ll n, ll val) {
    vi a(n+1);
    ll s = val;
    int idx = n;
    while (s!=0) {
        if (s > idx) {
            a[idx] = 1;
            s -= idx;
            idx--;
        }
        else {
            a[s] = 1;
            break;
        }
    }
    return a;
}

void place(vector<int>& arr, int l, int r, int i, vector<int>& A) {
    if (l > r) return;
    int idx = (l + r) / 2;
    arr[idx] = A[i];
    place(arr, l, idx - 1, 2 * i + 1, A);
    place(arr, idx + 1, r, 2 * i + 2, A);
}
struct Node
{
    int data;
    Node* left, * right;
};

/* Helper function that allocates a
new node */
Node* newNode(int data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}

// Function to insert nodes in level order
Node* insertLevelOrder(vector<int> arr,
    int i, int n)
{
    Node* root = nullptr;
    // Base case for recursion
    if (i < n)
    {
        root = newNode(arr[i]);

        // insert left child
        root->left = insertLevelOrder(arr,
            2 * i + 1, n);

        // insert right child
        root->right = insertLevelOrder(arr,
            2 * i + 2, n);
    }
    return root;
}

// Function to print tree nodes in
// InOrder fashion
void inOrder(Node* root, vector<int>& v)
{
    if (root != NULL)
    {
        inOrder(root->left, v);
        v.push_back(root->data);
        inOrder(root->right, v);
    }
}

bool ingrid(int x, int y, int n, int m) {
    if (x >= 0 && x < n && y >= 0 && y < m) return true;
    else return false;
}

set<string> st;
void solve(string s,int K){
    int cnt = 0;
    for (int i = 0; i < 7; i++) {
        if (s[i] == '?') cnt++;
    }
    if (cnt == 0) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += (s[i] - '0');
        }
        if (sum == K) st.insert(s);
        return;
    }

    int idx = 0;
    for (int i = 0; i < 7; i++) {
        if (s[i] == '?') {
            idx = i; break;
        }
    }
    string f = "";
    for (int i = 0; i < idx; i++) f = f + s[i];
    string g = "";
    for (int i = idx + 1; i < s.length(); i++) g = g + s[i];
    for (int i = 0; i <= 8; i++) {
        solve(f + to_string(i) + g, K);
    }

}

int maximumpoints(vector<vector<int> > arr, int n, int x, int y) {
    // code here
    vector<vector<int>> m(1001, vector<int>(1001, 0));
    for (int i = 0; i < n; i++) {
        m[arr[i][0]][arr[i][1]]++;
    }
    int ans = 0;
    for (int i = 1; i <= 1000; i++) {
        for (int j = 1; j <= 1000; j++) {
            m[i][j] += (m[i - 1][j] + m[i][j - 1] - m[i - 1][j - 1]);
            if (i - x > 0 && j - y > 0) {
                ans = max(ans, m[i][j] + m[i - x - 1][j - y - 1] - m[i][j - y - 1] - m[i - x - 1][j]);
            }
        }
    }
    return ans;
}

string func(string s, int k) {
    stack<pair<char,int>> st;
    st.push({ '#',0});
    for (int i = 0; i < s.length(); i++) {
        char c = s[i];
        if (c == st.top().first) {
            st.top().second++;
        }
        else {
            st.push({ c,1 });
        }

        if (st.top().second >= k) {
            st.pop();
        }
    }

    string ans = "";

    while (!st.empty()) {
        pair<char,int> p = st.top();
        st.pop();
        char ch = p.first;
        int num = p.second;
        for (int var = 0; var < num; var++) {
            ans = ch + ans;
        }
    }
    return ans;
}

bool cmp(pair<int, string> p1, pair<int, string> p2) {
    if (p1.first < p2.first) return true;
    /*else{
        if(p1.second.length()<p2.second.length()) return true;
        else{
            if(p1.second<p2.second) return true;
        }
    }*/
    return false;
}

bool strcmp(string s1, string s2) {
    if (s1.length() < s2.length()) return true;
    else {
        if (s1 < s2) return true;
    }
    return false;
}

bool cmp1(pair<int, int> p1, pair<int, int> p2) {
    if (p1.first != p2.first) return p1.first < p2.first;
    else p1.second < p2.second;
}

bool cmp2(pair<int, int> p1, pair<int, int> p2) {
    return p1.second < p2.second;
}

string MostFrequent(string str) {
    string s = "";
    map<string, int> mp;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] != ' ') s = s + str[i];
        else {
            mp[s]++;
            s = "";
        }
    }
    mp[s]++;
    map<string, int>::iterator it;
    string ans = "";
    int cnt = 0;
    for (it = mp.begin(); it != mp.end(); it++) {
        if (it->second > cnt) {
            ans = it->first;
            cnt = it->second;
        }
    }
    return ans;
}

vector<ll> reverse(vll vec) {
    int sz = vec.size();
    for (int i = 0; i < sz / 2; i++) vec[i] = vec[sz - 1 - i];
    return vec;
}
vector<ll> replace(vll vec) {
    int sz = vec.size();
    vll nv;
    rep(i, sz - 1) { nv.push_back(vec[i + 1] - vec[i]); }
    return nv;
}

#define print(vec) rep(i,vec.size()) cout<<vec[i]<<' ';
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    rep(test, t) {
        int n; cin >> n;
        vll a;
        ll ans = 0;
        rep(i, n) {
            int var; cin >> var; a.pb(var);
            ans += var;
        }
        for (int c = 0; c < n - 1; c++) {
            ans = max(ans, abs(a[a.size() - 1] - a[0]));
            a = replace(a);
        }
        cout << ans << endl;
    }

    return 0;
}

//Coded by:-suvampaul
#include<iostream>
#include<algorithm>
#include<vector>
#include<sstream>
#include<stack>
#include<deque>
#include<queue>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<numeric>
#include<string>
#include<iterator>
#include<limits>
using namespace std;

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define pii pair<int,int>
#define vpii vector<pii>
#define rep(i,n) for(int i=0; i<n; i++)
#define All(x,v) for(auto x:v)
#define arrin(a,n) ll a[n]; rep(i,n) cin>>a[i]
#define vi vector<int>
#define vll vector<ll>
#define nl "\n"
#define print(arr) All(x,arr) cout<<x<<" "; cout<<nl
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define ing(m,ad) rep(i,m){ int u,v; cin>>u>>v; u--;v--; ad[u].pb(v); ad[v].pb(u);}
#define vdsort(v) sort(v.begin(),v.end(),greater<int>())
#define F first
#define S second
#define mod 1'000'000'007

const int MAXN = 100'005;
int spf[MAXN];
ll fac1[MAXN], fac2[MAXN], inv[MAXN];
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <typename C, typename T = std::decay_t<decltype(*begin(std::declval<C>()))>, typename std::enable_if<!std::is_same<C, std::string>::value>::type* = nullptr>
std::ostream& operator<<(std::ostream& os, const C& container)
{
    bool first = true;
    std::stringstream ss;
    ss << '[';
    for (const auto& x : container)
    {
        if (!first)
        {
            ss << ", ";
        }
        first = false;
        ss << x;
    }
    ss << ']';
    return os << ss.str();
}
template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p)
{
    os << '{' << p.first << ", " << p.second << '}';
    return os;
}
vector<string> vec_splitter(string S)
{
    S += ',';
    vector<string> res;
    while (!S.empty())
    {
        res.push_back(S.substr(0, S.find(',')));
        S = S.substr(S.find(',') + 1);
    }
    return res;
}

template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T)
{
    if (idx > 0)
        cerr << ", ";
    else
        cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss;
    ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef DEBUG
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

bool isPerfectSquare(long double x)
{
    if (x >= 0) {
        long long sr = sqrt(x);
        return (sr * sr == x);
    }
    return false;
}

bool isPowerof2(int n, int) {
    return (n && !(n & n - 1));
}

string decimalToBin(ll N)
{
    string B_Number = "";
    int c = 0;
    while (N != 0) {
        if (N % 2 == 0) {
            B_Number = "0" + B_Number;
        }
        else B_Number = "1" + B_Number;
        N = N / 2;
        c++;
    }

    for (int i = c + 1; i <= 30; i++) {
        B_Number = "0" + B_Number;
    }

    return B_Number;
}

int binaryToDecimal(ll n)
{
    ll num = n;
    int dec_value = 0;
    int base = 1;
    ll temp = num;

    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;

        dec_value += last_digit * base;

        base = base * 2;
    }

    return dec_value;
}

ll min(ll a, ll b) {
    return a < b ? a : b;
}
ll max(ll a, ll b) {
    return a > b ? a : b;
}
ll power(ll a, ll b, ll Mod) {
    if (b == 0) {
        return 1;
    }
    ll ans = power(a, b / 2, Mod);
    ans *= ans;
    ans %= Mod;
    if (b % 2) {
        ans *= a;
    }
    return ans % Mod;
}

ll getMin(vector<ll> arr, int n)
{
    ll res = arr[0];
    for (int i = 1; i < n; i++)
        res = min(res, arr[i]);
    return res;
}
ll getMax(vector<ll> arr, int n)
{
    ll res = arr[0];
    for (int i = 1; i < n; i++)
        res = max(res, arr[i]);
    return res;
}

string adder(string s1, string s2) {
    vector<int> num;
    int len1 = s1.length();
    int len2 = s2.length();
    int len = max(len1, len2);
    for (int i = len1 + 1; i <= len; i++) s1 = "0" + s1;
    for (int i = len2 + 1; i <= len; i++) s2 = "0" + s2;
    for (int i = 0; i < len + 1; i++) num.push_back(0);
    int c = num.size();
    for (int i = 0; i < len; i++) {
        int x1 = s1[len - 1 - i] - '0';
        int x2 = s2[len - 1 - i] - '0';
        num[c - 1 - i] += (x1 + x2);
        num[c - 1 - i - 1] += num[c - 1 - i] / 10;
        num[c - 1 - i] = num[c - 1 - i] % 10;
    }
    string ans = "";
    int idx = 0;
    for (int i = 0; i < c; i++) {
        if (num[i] != 0) {
            idx = i; break;
        }
    }
    for (int i = idx; i < c; i++) {
        ans += to_string(num[i]);
    }
    return ans;
}

ll accurateFloor(ll a, ll b) {
    ll val = a / b;
    while (val * b > a)
        val--;
    return val;
}

vi formSum(ll n, ll val) {
    vi a(n+1);
    ll s = val;
    int idx = n;
    while (s!=0) {
        if (s > idx) {
            a[idx] = 1;
            s -= idx;
            idx--;
        }
        else {
            a[s] = 1;
            break;
        }
    }
    return a;
}

void place(vector<int>& arr, int l, int r, int i, vector<int>& A) {
    if (l > r) return;
    int idx = (l + r) / 2;
    arr[idx] = A[i];
    place(arr, l, idx - 1, 2 * i + 1, A);
    place(arr, idx + 1, r, 2 * i + 2, A);
}
struct Node
{
    int data;
    Node* left, * right;
};

/* Helper function that allocates a
new node */
Node* newNode(int data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}

// Function to insert nodes in level order
Node* insertLevelOrder(vector<int> arr,
    int i, int n)
{
    Node* root = nullptr;
    // Base case for recursion
    if (i < n)
    {
        root = newNode(arr[i]);

        // insert left child
        root->left = insertLevelOrder(arr,
            2 * i + 1, n);

        // insert right child
        root->right = insertLevelOrder(arr,
            2 * i + 2, n);
    }
    return root;
}

// Function to print tree nodes in
// InOrder fashion
void inOrder(Node* root, vector<int>& v)
{
    if (root != NULL)
    {
        inOrder(root->left, v);
        v.push_back(root->data);
        inOrder(root->right, v);
    }
}

bool ingrid(int x, int y, int n, int m) {
    if (x >= 0 && x < n && y >= 0 && y < m) return true;
    else return false;
}

set<string> st;
void solve(string s,int K){
    int cnt = 0;
    for (int i = 0; i < 7; i++) {
        if (s[i] == '?') cnt++;
    }
    if (cnt == 0) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += (s[i] - '0');
        }
        if (sum == K) st.insert(s);
        return;
    }

    int idx = 0;
    for (int i = 0; i < 7; i++) {
        if (s[i] == '?') {
            idx = i; break;
        }
    }
    string f = "";
    for (int i = 0; i < idx; i++) f = f + s[i];
    string g = "";
    for (int i = idx + 1; i < s.length(); i++) g = g + s[i];
    for (int i = 0; i <= 8; i++) {
        solve(f + to_string(i) + g, K);
    }

}

int maximumpoints(vector<vector<int> > arr, int n, int x, int y) {
    // code here
    vector<vector<int>> m(1001, vector<int>(1001, 0));
    for (int i = 0; i < n; i++) {
        m[arr[i][0]][arr[i][1]]++;
    }
    int ans = 0;
    for (int i = 1; i <= 1000; i++) {
        for (int j = 1; j <= 1000; j++) {
            m[i][j] += (m[i - 1][j] + m[i][j - 1] - m[i - 1][j - 1]);
            if (i - x > 0 && j - y > 0) {
                ans = max(ans, m[i][j] + m[i - x - 1][j - y - 1] - m[i][j - y - 1] - m[i - x - 1][j]);
            }
        }
    }
    return ans;
}

string func(string s, int k) {
    stack<pair<char,int>> st;
    st.push({ '#',0});
    for (int i = 0; i < s.length(); i++) {
        char c = s[i];
        if (c == st.top().first) {
            st.top().second++;
        }
        else {
            st.push({ c,1 });
        }

        if (st.top().second >= k) {
            st.pop();
        }
    }

    string ans = "";

    while (!st.empty()) {
        pair<char,int> p = st.top();
        st.pop();
        char ch = p.first;
        int num = p.second;
        for (int var = 0; var < num; var++) {
            ans = ch + ans;
        }
    }
    return ans;
}

bool cmp(pair<int, string> p1, pair<int, string> p2) {
    if (p1.first < p2.first) return true;
    /*else{
        if(p1.second.length()<p2.second.length()) return true;
        else{
            if(p1.second<p2.second) return true;
        }
    }*/
    return false;
}

bool strcmp(string s1, string s2) {
    if (s1.length() < s2.length()) return true;
    else {
        if (s1 < s2) return true;
    }
    return false;
}

bool cmp1(pair<int, int> p1, pair<int, int> p2) {
    if (p1.first != p2.first) return p1.first < p2.first;
    else p1.second < p2.second;
}

bool cmp2(pair<int, int> p1, pair<int, int> p2) {
    return p1.second < p2.second;
}

string MostFrequent(string str) {
    string s = "";
    map<string, int> mp;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] != ' ') s = s + str[i];
        else {
            mp[s]++;
            s = "";
        }
    }
    mp[s]++;
    map<string, int>::iterator it;
    string ans = "";
    int cnt = 0;
    for (it = mp.begin(); it != mp.end(); it++) {
        if (it->second > cnt) {
            ans = it->first;
            cnt = it->second;
        }
    }
    return ans;
}

vector<int> reverse(vi vec) {
    int sz = vec.size();
    for (int i = 0; i < sz / 2; i++) vec[i] = vec[sz - 1 - i];
    return vec;
}
vector<int> replace(vi vec) {
    int sz = vec.size();
    vi nv;
    rep(i, sz - 1) { nv.push_back(vec[i + 1] - vec[i]); }
    return nv;
}

#define print(vec) rep(i,vec.size()) cout<<vec[i]<<' ';
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    rep(test, t) {
        int n; cin >> n;
        vi a;
        ll ans = 0;
        rep(i, n) {
            int var; cin >> var; a.pb(var);
            ans += var;
        }
        for (int c = 0; c < n - 1; c++) {
            ans = max(ans, abs(a[a.size() - 1] - a[0]));
            a = replace(a);
        }
        cout << ans << endl;
    }

    return 0;
}

Answer: 1


#include <iostream>
#include<cmath>
#include<string>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        long long int arr[n];
        long long int ans=0;

        for(int i=0;i<n;i++){
            cin>>arr[i];
            ans +=arr[i];
        }
        if(n==1){
            cout<<arr[0]<<"\n";
            continue;
        }
        ans = abs(ans);
        for(int i=0;i<n-1;i++){
            int j=i;
            long long int pos_ans =0;
            for(int h=0;h<n-1-j;h++){
                arr[h]= arr[h+1] - arr[h];
                pos_ans += arr[h];
            }
            pos_ans = abs(pos_ans);
            ans = max(ans,pos_ans);
        }
        cout<<ans<<"\n";
    }
}

#include <iostream>
#include<cmath>
#include<string>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        long long int arr[n];
        long long int ans=0;

        for(int i=0;i<n;i++){
            cin>>arr[i];
            ans +=arr[i];
        }
        if(n==1){
            cout<<arr[0]<<"\n";
            continue;
        }

        for(int i=0;i<n-1;i++){
            int j=i;
            long long int pos_ans =0;
            for(int h=0;h<n-1-j;h++){
                arr[h]= arr[h+1] - arr[h];
                pos_ans += arr[h];
            }
            pos_ans = abs(pos_ans);
            ans = max(ans,pos_ans);
        }
        cout<<ans<<"\n";
    }
}

Answer: 2


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

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

void run_case(){
      int n;
      cin>>n;
      vector<ll>a(n+1);
      for(int i=1;i<=n;i++){
        cin>>a[i];
      }
      ll ans=LLONG_MIN;
      int curr=n;
      for(int i=1;i<=n;i++){
          ll sum=0;
          for(int j=1;j<=curr;j++){
            sum+=a[j];
          }
          if(i==1){
            ans=max(ans,sum);
          }
          else{
            ans=max(ans, max(sum, -sum));
          }
          for(int j=1;j<curr;j++){
            a[j]=a[j+1]-a[j];
          }
          curr--;
      }
      cout<<ans<<endl;

}

int main(){
      cin.tie(0)->sync_with_stdio(0);
       int tt;
       cin>>tt;

       while(tt--){
           run_case();
       }
}

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

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

void run_case(){
      int n;
      cin>>n;
      vector<int>a(n+1);
      for(int i=1;i<=n;i++){
        cin>>a[i];
      }
      ll ans=LLONG_MIN;
      int curr=n;
      for(int i=1;i<=n;i++){
          ll sum=0;
          for(int j=1;j<=curr;j++){
            sum+=a[j];
          }
          if(i==1){
            ans=max(ans,sum);
          }
          else{
            ans=max(ans, max(sum, -sum));
          }
          for(int j=1;j<curr;j++){
            a[j]=a[j+1]-a[j];
          }
          curr--;
      }
      cout<<ans<<endl;

}

int main(){
      cin.tie(0)->sync_with_stdio(0);
       int tt;
       cin>>tt;

       while(tt--){
           run_case();
       }
}

Answer: 1


for _ in range(int(input())):

    n = int(input())
    arr = list(map(int, input().split()))

    ans = sum(arr)

    while len(arr) > 1:
        arr = [arr[i+1]-arr[i] for i in range(len(arr)-1)]
        ans = max(ans, abs(sum(arr)))

    print(ans)

for _ in range(int(input())):

    n = int(input())
    arr = list(map(int, input().split()))

    ans = sum(arr)

    for i in range(n-1, -1, -1):
        if i > 1:
            arr = [arr[j+1]-arr[j] for j in range(i)]
            ans = max(ans, abs(sum(arr)))
    print(ans)

Answer: 1


mod = 1e9 + 7

def binpow(a, n):
    res = 1
    while n != 0:
        print(bin(n))
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res

def resheto(n):
    # n = int(input())
    primes = [int(a) for a in range(n + 1)]
    primes[1] = 0

    i = 2
    while i <= n:
        j = i + i
        while j <= n:
            primes[j] = 0
            j += i

        i += 1
    primes = [i for i in primes if i != 0]
    return primes

def delit(x):
    arr = []
    for i in range(2, int(x ** 0.5) + 1):
        if x % i == 0:
            arr.append(i)
            arr.append(x // i)
    return sorted(arr)

# n, k = 100000, 1
# arr = [10000] * 99999
# arr.append(-1)

def xfind(arr, x):
    r = len(arr) - 1
    l = 0
    while l <= r:
        mid = (l + r) // 2

        if arr[mid] == x:
            return True

        elif arr[mid] < x:
            l = mid + 1

        elif arr[mid] > x:
            r = mid - 1

    return False

from math import ceil
import bisect

from decimal import Decimal, getcontext

getcontext().prec = 15
#
# resh = resheto(10**6)

# def s(arr):
#     s = 0
#     for i in range(len(arr)):
#         mn = 1000000
#         for j in range(i, len(arr)):
#             mn = min(mn, arr[j])
#             s += mn
#     print(s)

from bisect import bisect_left, bisect_right

# def bs_l(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_left(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1
#
# def bs_r(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_right(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1

def pref_sum(arr):
    pref_sum_arr = [0] * len(arr)
    pref_sum_arr[0] = arr[0]
    for i in range(1, len(arr)):
        pref_sum_arr[i] = pref_sum_arr[i-1] + arr[i]
    return pref_sum_arr

def fibonacci(n):
    if n < 2:
        return n
    pp = 0
    p = 1
    for i in range(n - 1):
        pp, p = p, pp + p
    return p

import math
def fibonacci_binet(n):
    sqrt5 = math.sqrt(5)
    phi = (1 + sqrt5) / 2
    psi = (1 - sqrt5) / 2
    return round((phi**n - psi**n) / sqrt5)

from math import ceil
from bisect import bisect_left, bisect_right
from math import gcd, factorial
from decimal import Decimal
getcontext().prec = 4

def new_arr(arr):
    x = []
    for i in range(len(arr) - 1):
        x.append(arr[i+1] - arr[i])
    return x

def solve():
    n = int(input())
    arr = [int(a) for a in input().split()]
    while len(arr) > 1:
        rev_arr = arr[::-1]
        if len(arr) == 2 and arr[0] > 0 and arr[1] > 0:
            print(sum(arr))
            return
        if sum(new_arr(rev_arr)) < sum(new_arr(arr)):
            arr = new_arr(arr)
        else:
            arr = new_arr(rev_arr)
    print(sum(arr))

def main():
    t = int(input())
    for _ in range(t):
        # if _ == 152:
        #     n, k = map(int, input().split())
        #     s = input()
        #     print(s)
        # else:
        solve()

main()

mod = 1e9 + 7

def binpow(a, n):
    res = 1
    while n != 0:
        print(bin(n))
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res

def resheto(n):
    # n = int(input())
    primes = [int(a) for a in range(n + 1)]
    primes[1] = 0

    i = 2
    while i <= n:
        j = i + i
        while j <= n:
            primes[j] = 0
            j += i

        i += 1
    primes = [i for i in primes if i != 0]
    return primes

def delit(x):
    arr = []
    for i in range(2, int(x ** 0.5) + 1):
        if x % i == 0:
            arr.append(i)
            arr.append(x // i)
    return sorted(arr)

# n, k = 100000, 1
# arr = [10000] * 99999
# arr.append(-1)

def xfind(arr, x):
    r = len(arr) - 1
    l = 0
    while l <= r:
        mid = (l + r) // 2

        if arr[mid] == x:
            return True

        elif arr[mid] < x:
            l = mid + 1

        elif arr[mid] > x:
            r = mid - 1

    return False

from math import ceil
import bisect

from decimal import Decimal, getcontext

getcontext().prec = 15
#
# resh = resheto(10**6)

# def s(arr):
#     s = 0
#     for i in range(len(arr)):
#         mn = 1000000
#         for j in range(i, len(arr)):
#             mn = min(mn, arr[j])
#             s += mn
#     print(s)

from bisect import bisect_left, bisect_right

# def bs_l(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_left(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1
#
# def bs_r(a, x, lo=0, hi=None):
#     if hi is None: hi = len(a)
#     pos = bisect_right(a, x, lo, hi)
#     return pos if pos != hi and a[pos] == x else -1

def pref_sum(arr):
    pref_sum_arr = [0] * len(arr)
    pref_sum_arr[0] = arr[0]
    for i in range(1, len(arr)):
        pref_sum_arr[i] = pref_sum_arr[i-1] + arr[i]
    return pref_sum_arr

def fibonacci(n):
    if n < 2:
        return n
    pp = 0
    p = 1
    for i in range(n - 1):
        pp, p = p, pp + p
    return p

import math
def fibonacci_binet(n):
    sqrt5 = math.sqrt(5)
    phi = (1 + sqrt5) / 2
    psi = (1 - sqrt5) / 2
    return round((phi**n - psi**n) / sqrt5)

from math import ceil
from bisect import bisect_left, bisect_right
from math import gcd, factorial
from decimal import Decimal
getcontext().prec = 4

def new_arr(arr):
    x = []
    for i in range(len(arr) - 1):
        x.append(arr[i+1] - arr[i])
    return x

def solve():
    n = int(input())
    arr = [int(a) for a in input().split()]
    ans = sum(arr)

    while len(arr) > 1:
        rev_arr = new_arr(arr[::-1])
        arr = new_arr(arr)
        ans = max(ans, sum(arr), sum(rev_arr))

    print(ans)

def main():
    t = int(input())
    for _ in range(t):
        # if _ == 152:
        #     n, k = map(int, input().split())
        #     s = input()
        #     print(s)
        # else:
        solve()

main()

Answer: 2


/* Gauri Jain
   2312004 */

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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define endl "\n"
#define ll long long
#define EPS 1e-9
#define fl(a, b) for (int i = a; i < b; i++)
#define getarr(n) int* arr = new int[n]; fl(i, n) { cin >> arr[i]; }
#define getv(v, n) vector<int> v; fl(0, n) { int el; cin >> el; v.push_back(el); }
#define sortv(v) sort(v.begin(), v.end())

/* Debug Code */
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename A>
void __print(const A &x);
template <typename A, typename B>
void __print(const pair<A, B> &p);
template <typename T>
void __print(stack<T> s);
template <typename T>
void __print(queue<T> q);
template <typename T, typename... U>
void __print(priority_queue<T, U...> q);

template <typename A>
void __print(const A &x) {
    bool first = true;
    cerr << '{';
    for (const auto &i : x) {
        cerr << (first ? "" : ",");
        __print(i);
        first = false;
    }
    cerr << '}';
}

template <typename A, typename B>
void __print(const pair<A, B> &p) {
    cerr << '(';
    __print(p.first);
    cerr << ',';
    __print(p.second);
    cerr << ')';
}

template <typename T>
void __print(stack<T> s) {
    vector<T> debugVector;
    while (!s.empty()) {
        T t = s.top();
        debugVector.push_back(t);
        s.pop();
    }
    reverse(debugVector.begin(), debugVector.end());
    __print(debugVector);
}

template <typename T>
void __print(queue<T> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.front();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}

template <typename T, typename... U>
void __print(priority_queue<T, U...> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.top();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}

void _print() { cerr << "]\n"; }

template <typename Head, typename... Tail>
void _print(const Head &H, const Tail &...T) {
    __print(H);
    if (sizeof...(T))
        cerr << ", ";
    _print(T...);
}

bool isInteger(double num) {
    return floor(num) == num;
}

void print(vector<int> &arr) {
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

string decimalToBinary(int n) {
    if (n == 0) return "0";
    string binary = "";
    while (n > 0) {
        binary += std::to_string(n % 2);
        n /= 2;
    }
    reverse(binary.begin(), binary.end());
    return binary;
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "Line: " << __LINE__ << " [" << #__VA_ARGS__ << "] = [", _print(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve(){
    int n; cin >> n;
    vector<ll> a(n);
    for(int i=0; i<n; i++) cin >> a[i];
    ll sum = 0, maxi = LLONG_MIN;
    if(n == 1){
        cout << a[0] << endl;
        return;
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n-i; j++){
            sum += a[j];
            if(j+1<n)a[j] = a[j] - a[j+1];
        }
        if(i == 0) maxi = sum;
        else maxi = max(maxi, abs(sum));
        sum = 0;
    }
    cout << maxi << endl;
    return;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

/* Gauri Jain
   2312004 */

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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define endl "\n"
#define ll long long
#define EPS 1e-9
#define fl(a, b) for (int i = a; i < b; i++)
#define getarr(n) int* arr = new int[n]; fl(i, n) { cin >> arr[i]; }
#define getv(v, n) vector<int> v; fl(0, n) { int el; cin >> el; v.push_back(el); }
#define sortv(v) sort(v.begin(), v.end())

/* Debug Code */
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename A>
void __print(const A &x);
template <typename A, typename B>
void __print(const pair<A, B> &p);
template <typename T>
void __print(stack<T> s);
template <typename T>
void __print(queue<T> q);
template <typename T, typename... U>
void __print(priority_queue<T, U...> q);

template <typename A>
void __print(const A &x) {
    bool first = true;
    cerr << '{';
    for (const auto &i : x) {
        cerr << (first ? "" : ",");
        __print(i);
        first = false;
    }
    cerr << '}';
}

template <typename A, typename B>
void __print(const pair<A, B> &p) {
    cerr << '(';
    __print(p.first);
    cerr << ',';
    __print(p.second);
    cerr << ')';
}

template <typename T>
void __print(stack<T> s) {
    vector<T> debugVector;
    while (!s.empty()) {
        T t = s.top();
        debugVector.push_back(t);
        s.pop();
    }
    reverse(debugVector.begin(), debugVector.end());
    __print(debugVector);
}

template <typename T>
void __print(queue<T> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.front();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}

template <typename T, typename... U>
void __print(priority_queue<T, U...> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.top();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}

void _print() { cerr << "]\n"; }

template <typename Head, typename... Tail>
void _print(const Head &H, const Tail &...T) {
    __print(H);
    if (sizeof...(T))
        cerr << ", ";
    _print(T...);
}

bool isInteger(double num) {
    return floor(num) == num;
}

void print(vector<int> &arr) {
    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

string decimalToBinary(int n) {
    if (n == 0) return "0";
    string binary = "";
    while (n > 0) {
        binary += std::to_string(n % 2);
        n /= 2;
    }
    reverse(binary.begin(), binary.end());
    return binary;
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "Line: " << __LINE__ << " [" << #__VA_ARGS__ << "] = [", _print(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve(){
    int n; cin >> n;
    vector<ll> a(n);
    for(int i=0; i<n; i++) cin >> a[i];
    ll sum = 0, maxi = LLONG_MIN;
    if(n == 1){
        cout << a[0] << endl;
        return;
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n-i; j++){
            sum += a[j];
            if(j+1<n)a[j] = a[j] - a[j+1];
        }
        maxi = max(maxi, abs(sum));
        sum = 0;
    }
    cout << maxi << endl;
    return;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


// 主函数，程序的入口点
#include <bits/stdc++.h>
// #define int long long
#define ios ios::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;
const int N = 2e5 + 7, modd = 998244353, mod = 1e9 + 7, inf = 0x3f3f3f;
#define endl '\n'
#define ll long long
#define int long long
signed main()
{
    // 定义变量 t，用于存储测试数据的组数
    int t;
    // 从标准输入读取测试数据的组数
    cin >> t;
    // 循环处理每组测试数据
    while (t--)
    {
        // 定义变量 n，用于存储当前测试数据中序列的长度
        int n;
        // 从标准输入读取序列的长度
        cin >> n;
        // 定义数组 a，用于存储序列的元素
        vector<int> a(n + 1);
        // 循环读取序列的每个元素，并存储在数组 a 中
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        // 初始化当前序列的长度为 n
        int now = n;
        // 初始化最终结果为一个极小值
        long long ans = -1e18;
        // 主循环，控制处理的轮数
        for (int i = 1; i <= n; i++)
        {
            // 初始化当前序列的和为 0
            long long sum = 0;
            // 计算当前序列的和
            for (int j = 1; j <= now; j++)
                sum = (sum + a[j]);
            // 如果是第一轮处理，直接更新 ans 为 ans 和 sum 中的较大值
            if (i == 1)
                ans = max(ans, sum);
            // 否则，更新 ans 为 ans、sum 和 -sum 中的最大值
            else
                ans = max(ans, max(sum, (-sum)));
            // 对当前序列进行差分操作
            for (int j = 1; j < now; j++)
                a[j] = (a[j + 1] - a[j]);
            // 序列长度减 1
            now--;
        }
        // 输出最终的最大值
        cout << ans << endl;
    }
    // 程序正常结束
    return 0;
}

// 主函数，程序的入口点
#include <bits/stdc++.h>
// #define int long long
#define ios ios::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;
const int N = 2e5 + 7, modd = 998244353, mod = 1e9 + 7, inf = 0x3f3f3f;
#define endl '\n'
#define ll long long
int main()
{
    // 定义变量 t，用于存储测试数据的组数
    int t;
    // 从标准输入读取测试数据的组数
    cin >> t;
    // 循环处理每组测试数据
    while (t--)
    {
        // 定义变量 n，用于存储当前测试数据中序列的长度
        int n;
        // 从标准输入读取序列的长度
        cin >> n;
        // 定义数组 a，用于存储序列的元素
        vector<int> a(n + 1);
        // 循环读取序列的每个元素，并存储在数组 a 中
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        // 初始化当前序列的长度为 n
        int now = n;
        // 初始化最终结果为一个极小值
        long long ans = -1e18;
        // 主循环，控制处理的轮数
        for (int i = 1; i <= n; i++)
        {
            // 初始化当前序列的和为 0
            long long sum = 0;
            // 计算当前序列的和
            for (int j = 1; j <= now; j++)
                sum = (sum + a[j]);
            // 如果是第一轮处理，直接更新 ans 为 ans 和 sum 中的较大值
            if (i == 1)
                ans = max(ans, sum);
            // 否则，更新 ans 为 ans、sum 和 -sum 中的最大值
            else
                ans = max(ans, max(sum, (-sum)));
            // 对当前序列进行差分操作
            for (int j = 1; j < now; j++)
                a[j] = (a[j + 1] - a[j]);
            // 序列长度减 1
            now--;
        }
        // 输出最终的最大值
        cout << ans << endl;
    }
    // 程序正常结束
    return 0;
}

Answer: 1


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF = 1e18;
void solve(){
    int n;cin >> n;
    vector<int>a(n+1,0);
    for(int i = 1;i <= n;i++) cin >> a[i];
    if(n==1){
        cout<<a[1]<<"\n";
        return ;
    }
    LL ans = -INF;
    for(int i = 1;i <= n;i++){
        LL sum = 0;
        for(int j = 1;j <= n-i+1;j++) sum += a[j];
        ans = max(ans,max(sum,-sum));
        for(int j = 1;j <= n-i;j++) a[j] = a[j+1] - a[j];
    }
    cout<<ans<<"\n";
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int T = 1;cin >> T;
    while(T-- )solve();
    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF = 1e18;
void solve(){
    int n;cin >> n;
    vector<LL>a(n+1,0);
    for(int i = 1;i <= n;i++) cin >> a[i];
    if(n==1){
        cout<<a[1]<<"\n";
        return ;
    }
    LL ans = -INF;
    for(int i = 1;i <= n;i++){
        LL sum = 0;
        for(int j = 1;j <= n-i+1;j++) sum += a[j];
        if(i==1)ans = max(ans,sum);
        else ans = max(ans,max(sum,-sum));
        for(int j = 1;j <= n-i;j++) a[j] = a[j+1] - a[j];
    }
    cout<<ans<<"\n";
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int T = 1;cin >> T;
    while(T-- )solve();
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        int k=1;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            if(!k) res=max(res,abs(x));
            else{
                res=max(res,x);k=0;
            }

        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        int k=1;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            if(!k) res=max(res,abs(x));
            else res=max(res,x);
            k--;
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}

Answer: 1


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

#define ll long long
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define nl cout << '\n';
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mod 1000000007
#define make(n) ll n; cin >> n;
#define makech(ch) char ch; cin >> ch;
#define makestr(s) string s; cin >> s;
#define show(n) cout << n << endl;
#define makearr(a, n) vector<ll> a(n); for (int i = 0; i < n; i++){ cin >> a[i]; }
#define make2d(a, n, m) vector<vector<ll>> a(n, vector<ll>(m)); for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ cin >> a[i][j]; } }
#define all(v) v.begin(), v.end()
#define showarr(a, n) for (int i = 0; i < n; i++){ cout << a[i] << " "; } cout << '\n';
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>

vector<ll> generatePascalsTriangleLine(int n) {
    vector<ll> line(n + 1, 1);
    for (int i = 1; i < n; i++){
        line[i] = line[i - 1] * (n - i + 1) / i;
    }
    return line;
}

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

    make(t);
    while(t--){
        make(n);
        makearr(arr, n)
        ll sum = 0; for (int i = 0; i < n; i++) sum += arr[i];
        if(n == 1){ show(sum) continue; }

        vector<ll> gpt = generatePascalsTriangleLine(n - 1);

        ll F_sum = 0, G_sum = 0;
        for (int i = 0; i < n; i++){
            F_sum += ((n - 1 - i) % 2 ? -1LL : 1LL) * gpt[i] * arr[i];
            G_sum += (i % 2 ? -1LL : 1LL) * gpt[i] * arr[i];
        }
        ll ans = max({sum, (ll)abs(F_sum), (ll)abs(G_sum)});
        show(ans)
    }
    return 0;
}

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

#define ll long long
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define nl cout << '\n';
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mod 1000000007
#define make(n) ll n; cin >> n;
#define makech(ch) char ch; cin >> ch;
#define makestr(s) string s; cin >> s;
#define show(n) cout << n << endl;
#define makearr(a, n) vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; }
#define make2d(a, n, m) vvi a(n, vector<ll>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } }
#define all(v) v.begin(), v.end()
#define showme(a) for(auto it:a){cout<<it<<" ";} cout<<'\n';
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define forl(n) for(int i=0; i<n; i++)

vector<ll> generatePascalsTriangleLine(int n) {
    vector<ll> line(n + 1, 1);
    for (int i = 1; i < n; i++) {
        line[i] = line[i - 1] * (n - i + 1) / i;
    }
    return line;
}

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

    make(t);
    while (t--) {
        make(n);
        makearr(arr, n)

        ll sum = 0, ans = 0;
        for(int i=0; i<arr.size(); i++){
            sum+=arr[i];
        }
        ans=sum;

        vector<ll>curr=arr;
        while(curr.size()>1){
            vector<ll>diff;
            for(int i=0; i<curr.size()-1; i++){
                diff.pb(curr[i+1]-curr[i]);
            }

            sum=accumulate(all(diff), 0LL);
            ans=max(ans, abs(sum));

            curr.clear();
            for(int i=0; i<diff.size(); i++){
                curr.pb(diff[i]);
            }
            diff.clear();
        }
        show(ans)
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int long long
int sum(vector<int>&v){
    int tot = 0;
    for(int i = 1;i < v.size();i++)
        tot += v[i] - v[i - 1] ;
    return tot;
}
int sumall(vector<int>&v){
    int sum = 0;
    for(int i = 0;i < v.size();i++){
        sum +=v[i];
    }
    return sum;
}
void _()
{
    int n; cin >> n;
    vector<int>v(n);
    for(int i = 0;i < n;i++)
        cin >> v[i];
    int res = -1e10;
    while(!v.empty()){
        res = max(res,sumall(v));
        vector<int>a = v,b = v;
        reverse(a.begin(),a.end());
        if(sum(a) > sum(b)){
            reverse(v.begin(),v.end());
        }
        for(int i = 0;i < v.size() - 1;i++){
            v[i] = v[i + 1] - v[i];
        }
        v.pop_back();
    }
    cout << res << endl;
}
signed main()
{
    int t = 1;cin >> t;
    while(t--)
    {
        _();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
int sum(vector<int>&v){
    int tot = 0;
    for(int i = 0;i < v.size() - 1;i++)
        tot += v[i] - v[i + 1] ;
    return tot;
}
int sumall(vector<int>&v){
    int sum = 0;
    for(int i = 0;i < v.size();i++){
        sum +=v[i];
    }
    return sum;
}
void _()
{
    int n; cin >> n;
    vector<int>v(n);
    for(int i = 0;i < n;i++)
        cin >> v[i];
    int res = -1e10;
    while(!v.empty()){
        res = max(res,sumall(v));
        vector<int>a = v,b = v;
        reverse(a.begin(),a.end());
        if(sum(a) > sum(b)){
            reverse(v.begin(),v.end());
        }
        for(int i = 0;i < v.size() - 1;i++){
            v[i] = v[i + 1] - v[i];
        }
        v.pop_back();
    }
    cout << res << endl;
}
signed main()
{
    int t = 1;cin >> t;
    while(t--)
    {
        _();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 50+5;
ll T,n,a[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(false),cout.tie(false);
	cin>>T;
	while(T--){
		cin>>n;
		ll sum=0,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		maxx=sum;
		for(int i=2;i<=n;i++){
			sum=0;
			for(int j=n;j>=i;j--){
				a[j]=a[j]-a[j-1];
				sum+=a[j];
			}
			maxx=max(maxx,(sum<0 ? -sum : sum));
		}
		cout<<maxx<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 50+5;
ll T,n,a[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>T;
	while(T--){
		cin>>n;
		ll sum=0,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		maxx=sum;
		for(int i=2;i<=n;i++){
			sum=0;
			for(int j=n;j>=i;j--){
				a[j]=a[j]-a[j-1];
				sum+=a[j];
			}
			maxx=max(maxx,(sum<0 ? -sum : sum));
		}
		cout<<maxx<<endl;
	}
	return 0;
}

Answer: 2


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

using namespace std;

vector<int> replace( vector <int> a){
    int size = a.size();
    vector<int> rep_a;

    for( int i = 1;i < size; i++){
        rep_a.push_back(a[i] - a[i-1]);
    }

    return rep_a;

}

int sum_array( vector<int> a){
    int sum = 0;
    for( int i =0; i < a.size(); i++){

        sum += a[i];
    }

    return abs(sum);

}

int max_sum_sequence(int n , vector <int>& a){
    if (n == 1) {
        return a[0];
    } else if (n == 2) {
        return max(a[0] + a[1], abs(a[0] - a[1]));
    } else {
        int sum = 0;
        sum = sum_array(a);

        for( int j =0; j < n-1; j++ ){
        int rep_sum = 0;
        a = replace(a);
        rep_sum = sum_array(a);
        if( rep_sum > sum ) sum = rep_sum;

        }

            return sum;
    }
}

int main(){
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        int result = max_sum_sequence(n, a);
        cout << result << endl;
    }
    return 0;
}

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

using ll = long long;

using namespace std;

vector<ll> replace( vector <ll> a){
    ll size = a.size();
    vector<ll> rep_a;

    for( ll i = 1;i < size; i++){
        rep_a.push_back(a[i] - a[i-1]);
    }

    return rep_a;

}

ll sum_array( vector<ll> a){
    ll sum = 0;
    for( ll i =0; i < a.size(); i++){

        sum += a[i];
    }

    return sum;

}

ll max_sum_sequence(ll n , vector <ll>& a){
    vector<ll> temp;

    if (n == 1) {
        return a[0];
    } else {
        ll sum = 0;
        sum = sum_array(a);

        for( ll j =0; j < n-1; j++ ){
        ll rep_sum = 0;
        temp = replace(a);
        a = temp;
        rep_sum = abs(sum_array(a));
        if( rep_sum > sum ) sum = rep_sum;
        }

            return sum;
    }
}

int main(){
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        vector<ll> a(n);
        for (ll i = 0; i < n; ++i) {
            cin >> a[i];
        }
        ll result = max_sum_sequence(n, a);
        cout << result << endl;
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define BOOST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

using namespace std;

// Convert a vector of ints into a string key for memoization.
string vecToKey(const vector<int>& arr) {
    // We join numbers with a comma.
    string key;
    for (int x : arr) {
        key += to_string(x) + ",";
    }
    return key;
}

unordered_map<string, ll> memo;

ll F(const vector<int>& arr) {
    // Casos base
    string key = vecToKey(arr);
    if (memo.count(key))
        return memo[key];       // memoizacao

    if (arr.size() == 1)
        return memo[key] = arr[0];

    // Recursao
    // Soma do original
    ll sum1 = 0;
    for (int x : arr)
        sum1 += x;

    // Soma do invertido
    vector<int> rev(arr);
    reverse(rev.begin(), rev.end());
    ll sum2 = 0;
    for (int x : rev)
        sum2 += x;

    // Sequencia original
    vector<int> diff1;
    for (size_t i = 0; i < arr.size()-1; i++){
        diff1.push_back(arr[i+1] - arr[i]);
    }

    // Sequencia invertida
    vector<int> diff2;
    for (size_t i = 0; i < rev.size()-1; i++){
        diff2.push_back(rev[i+1] - rev[i]);
    }

    ll ans = max({ sum1, sum2, F(diff1), F(diff2) });
    memo[key] = ans;
    return ans;
}

int main(){
    BOOST

    int t;
    cin >> t;
    while (t--){
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; i++){
            cin >> arr[i];
        }
        memo.clear();
        cout << F(arr) << "\n";
    }
    return 0;
}

/**
 * SUGERIDO:
 */

#include <bits/stdc++.h>
#define BOOST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

using namespace std;

int main(){
    BOOST

    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<ll> arr(n);
        for (int i = 0; i < n; i++){
            cin >> arr[i];
        }

        // Se n = 1, nao precisa processar
        if(n == 1){
            cout << arr[0] << "\n";
            continue;
        }

        // Se k = 0: nao calcula subtracoes
        ll sum0 = 0;
        for (ll x : arr)
            sum0 += x;
        ll best = sum0; // Nota, nao inverter para k=0

        vector<ll> current = arr;

        // Para cada k de 1 ate n-1, aplicar operacao 2 ("Replace the sequence with its difference sequence")
        for (int k = 1; k < n; k++){
            vector<ll> next;
            for (int i = 0; i < (int)current.size() - 1; i++){
                next.push_back(current[i+1] - current[i]);
            }
            // Soma da diferenca
            ll s = 0;
            for (ll x : next)
                s += x;

            // Para k >= 1 ver com o sinal trocado (se s negativo, inversao da positivo - llabs)
            best = max(best, llabs(s));

            // Proxima iteracao
            current = next;
        }

        cout << best << "\n";
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
int a[1000005],n;

void solve() {
	cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		ans += a[i];
	}
	if (n == 1) {
		cout << ans << endl;//不能进行1，2操作
        return;
	}
	while (n) {
		n--;
		for (int i = 1; i <= n; i++) {
			a[i] = a[i + 1] - a[i];//2操作
		}
		int sum = 0;
		for (int i = 1; i <= n; i++) sum += a[i];
		ans = max(ans, max(sum, -sum));//是否进行1操作
	}
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int tt;
	cin >> tt;
	while (tt--)
		solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
int a[1000005],n;

void solve() {
	cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		ans += a[i];
	}
	if (n == 1) {
		cout << ans << endl;//不能进行1，2操作
	}
	while (n) {
		n--;
		for (int i = 1; i <= n; i++) {
			a[i] = a[i + 1] - a[i];//2操作
		}
		int sum = 0;
		for (int i = 1; i <= n; i++) sum += a[i];
		ans = max(ans, max(sum, -sum));//是否进行1操作
	}
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int tt;
	cin >> tt;
	while (tt--)
		solve();
	return 0;
}

Answer: 1


#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;
}

#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
long long fac(vector<int> &dp,int n,int i,int a){
    if(n<=i){
        return 1;
    }

    return n*fac(dp,n-1,i,a-1)/a;
}
int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        bool f=false;
        vector<int> arr(n);
        for(int i=0;i<n;i++){
            cin>>arr[i];
            if(arr[i]<=0){
               f = true;
            }
        }
        if(n==1){
            cout<<arr[0]<<endl;
            continue;
        }
        vector<int> dp(n+1);
        for(auto x:dp)x=-1;
        long long sum=0;
        if(f){
        for(int i=0;i<n;i++){
            sum += fac(dp,n-1,n-i-1,i)*(arr[i])*pow(-1,i);
        }
        }
        else{
            for(int i=0;i<n;i++){
                sum+=arr[i];
            }
        }
        cout<<abs(sum)<<endl;

    }
    return 0;
}

Answer: 1


import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)

def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = sum(a)
    while len(a) > 1:
        ans = max(ans, abs(a[0] - a[-1]))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    return ans

for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")

import sys

input = lambda: sys.stdin.readline().rstrip("\r\n")

INF = int(1e18)

def solve():
    N = int(input())
    a = [int(v) for v in input().split()]
    ans = -INF
    while len(a) > 1:
        ans = max(ans, abs(sum(a)))
        a = [a[i] - a[i - 1] for i in range(1, len(a))]
    ans = max(ans, abs(a[0]))
    return ans

for _ in range(int(input())):
    # solve()
    print(solve())
    # print("YES" if solve() else "NO")

Answer: 1


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
#define endl "\n"
void solve(){
	int n;
	cin >> n;
	vector<int>a(n+1);
	for(int i=1;i<=n;i++)cin >> a[i];
	if(n==1){
		cout << a[1] << endl;
		return ;
	}
	int ans=-1e9;
	for(int i=n-1;i>=1;i--){
		int  t=0;
		for(int j=1;j<=i+1;j++)t+=a[j];
		if(i==n-1)ans=max({ans,t});
		else{
			ans=max({ans,-t,t});
		}
		for(int j=1;j<=i;j++){
			a[j]=a[j+1]-a[j];
		}
	}
	cout << max({ans,a[1],-a[1]}) << endl;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int _=1;
	cin >> _;
	while(_--)
		solve();
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
#define endl "\n"
void solve(){
	int n;
	cin >> n;
	vector<int>a(n+1);
	for(int i=1;i<=n;i++)cin >> a[i];
	if(n==1){
		cout << a[1] << endl;
		return ;
	}
	int ans=-1e9;
	for(int i=n-1;i>=1;i--){
		int  t=0;
		for(int j=1;j<=i+1;j++)t+=a[j];
		ans=max({ans,t});
		for(int j=1;j<=i;j++){
			a[j]=a[j+1]-a[j];
		}
	}
	cout << max({ans,a[1],-a[1]}) << endl;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int _=1;
	cin >> _;
	while(_--)
		solve();
}

Answer: 1


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

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    int sum = 0;
    for(int &i:a)
    {
        cin >> i;
        sum += i;
    }
    if (n == 1)
    {
        cout << a[0] << endl;
        return;
    }
    for (int i = 0; i < n - 1; ++i)
    {
        for(int j=0;j<a.size()-1;++j)
            a[j]=a[j+1]-a[j];
        a.pop_back();
        int temp = 0;
        for(int j=0;j<a.size();++j)
            temp+=a[j];
        if(abs(temp)>abs(sum) )
        {
            sum=temp;
        }
    }
    cout<<abs(sum)<<endl;
}

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

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

void solve()
{
	long long a[1010];
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	int now = n;
	long long ans = -1e18;
	for (int i = 1; i <= n; i++) {
		long long sum = 0;
		for (int i = 1; i <= now; i++) sum = (sum + a[i]);
		if (i == 1) ans = max(ans, sum);
		else ans = max(ans, max(sum, (-sum)));
		for (int i = 1; i < now; i++) a[i] = (a[i + 1] - a[i]);
		now--;
	}
	cout << ans << endl;
}

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

Answer: 2


#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e9 + 10;
const ll INF = 1e18 + 100;
const int mod = 998244353;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    ll ans = -INF;
    int now = n;
    for (int i = 1; i <= n; i++) {
        ll sum = 0;
        for (int i = 1; i <= now; i++) sum += a[i];
        if (i == 1) ans = max(ans, sum);
        else ans = max(ans, abs(sum));
        for (int j = 1; j < now; j++)
            a[j] = a[j + 1] - a[j];
        now--;
    }
    cout << ans << '\n';
}

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

    int T = 1;
    cin >> T;
    while (T--)
        solve();

    return 0;
}
/*

1
4
1 1 1 1
1 1 1 1
1 1 1 2
1 1 1 2

*/

#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1e9 + 10;
const int INF = 1e9 + 100;
const int mod = 998244353;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    int ans = -INF, now = n;
    for (int i = 1; i <= n; i++) {
        int sum = accumulate(a.begin(), a.begin() + now + 1, 0);
        if (i == 1) ans = max(ans, sum);
        else ans = max(ans, abs(sum));
        for (int j = 1; j < now; j++)
            a[j] = a[j + 1] - a[j];
        now--;
    }
    cout << ans << '\n';
}

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

    int T = 1;
    cin >> T;
    while (T--)
        solve();

    return 0;
}
/*

1
4
1 1 1 1
1 1 1 1
1 1 1 2
1 1 1 2

*/

Answer: 1


#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
int _, n, ans, sum, a[51], b[51];
int main() {
	scanf("%d", &_);
	for (; _--; ) {
		ans = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]), ans += a[i];
		if (n == 1) {
			printf("%d\n", a[1]);
			continue;
		}
		for (; n > 1; ) {
			sum = 0;
			for (int i = 1; i < n; i++)
				b[i] = a[i + 1] - a[i], sum += b[i];
			ans = max(ans, abs(sum));
			--n;
			for (int i = 1; i <= n; i++)
				a[i] = b[i];
		}
		printf("%d\n", ans);
	}
}

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
int _, n;
ll ans, sum, a[51];
int main() {
	scanf("%d", &_);
	for (; _--; ) {
		ans = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
			scanf("%lld", &a[i]), ans += a[i];
		for (int i = 1; i < n; i++) {
			sum = 0;
			for (int j = 1; i + j <= n; j++)
				a[j] -= a[j + 1], sum += a[j];
			if (sum < 0)
				sum *= -1;
			ans = max(ans, sum);
		}
		printf("%lld\n", ans);
	}
}

Answer: 2