ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 203 ms):
#include <bits/stdc++.h>

using namespace std;

#define ll as long long;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> count = {1};
  for (int i = 0; i < n; ++i) cin >> a[i];
  sort(a.begin(), a.end()); 
  for (int i = 1; i < n; ++i) {
    if (a[i] == a[i-1]) {
      count.back()++;
    } else {
      count.push_back(1);
    }
  }
  sort(count.begin(), count.end());
  int m = size(count);
  for (int i = 0; i < m; ++i) {
    if (count[i] > k) {
      cout << m - i << '\n';
      return;
    } 
    k -= count[i];
  }
  cout << 1 << '\n';
}

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


Solution 2 (runtime: 140 ms):
#include <bits/stdc++.h>
using namespace std;
 
#define arrdec ll n;cin >> n;ll a[n];for (ll i = 0; i < n; i++) cin >> a[i];
#define f(g, h) for (ll i = g; i < h; i++)
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define py cout << "YES\n"
#define pm cout << "-1\n"
#define pn cout << "NO\n"
#define pll pair<ll, ll>
#define N 1000000
void solve() 
{
    ll n, k; cin >> n >> k;
    ll a[n]; f(0, n) cin >> a[i];
    map<ll, ll> m;
    f(0, n){
        m[a[i]]++;
    }

    vector<pair<ll, ll>> v;
    for(auto i:m){
        v.pb({i.second, i.first});
    }

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

    int c = 0;
    int flag = 0;
    for(auto i:v){
        if(k>=i.first){
            k-=i.first;
            c++;
        }else{
            // c++;
            // flag = 1;
            // k = 0;
            break;
        }
        // if(flag == 1){
        //     // c++;
        // }
        
    }
    // cout << c << v.size()<<endl;
    int ans = v.size()-c;
    // if(k!=0) ans++;
    // cout << v.size() - 1
    if(ans==0){ans++;}
    cout << ans << endl;

}
 
 
int main()
{
 
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t = 1;
    cin >> t;
 
    for (ll i = 1; i <= t; i++)
    {
        solve();
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;

// Typedefs for convenience
using ll = long long;
using ull = unsigned long long;
using ld = long double;

// Constants
const int MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const ll LINF = 1e18;

// Utility macros
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define vecIn(n, arr) for (int i = 0; i < (n); i++) { int x; cin >> x; arr.push_back(x); }
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define sz(v) (int)(v).size()

// Debugging tools
#ifdef LOCAL
#define debug(x) cerr << #x << ": " << (x) << "\n";
#else
#define debug(x)
#endif

// Modular arithmetic
inline int add(int a, int b, int mod = MOD) {
    return (a + b) % mod;
}
inline int sub(int a, int b, int mod = MOD) {
    return (a - b + mod) % mod;
}
inline int mul(int a, int b, int mod = MOD) {
    return (ll(a) * b) % mod;
}
inline int power(int a, int b, int mod = MOD) {
    int res = 1;
    while (b) {
        if (b & 1) res = mul(res, a, mod);
        a = mul(a, a, mod);
        b >>= 1;
    }
    return res;
}
inline int mod_inv(int a, int mod = MOD) {
    return power(a, mod - 2, mod);
}

// Input/output helpers
template <typename T> void print_vec(const vector<T>& v) {
    for (const auto& x : v) cout << x << " ";
    cout << "\n";
}


// Solve function
void solve() {
    // Your problem-solving logic here
    int n;
    cin>>n;
    vector<int> arr;
    vecIn(n, arr);
    for(int i = 1; i < n; i++) {
        int a = arr[i - 1];
        int b = arr[i];
        int c = min(a ,b);
        a -= c;
        b -= c;
        if(b == 0 && a != 0) {
            cout<<"NO"<<endl;
            return;
        }
        arr[i - 1] = a;
        arr[i] = b;
    }
    cout<<"YES"<<endl;
}

// Main function
int main() {
    fastio();

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

    return 0;
}

Solution 2 (runtime: 374 ms):
#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;



int main() {

    int t;

    cin >> t;

    

    while (t--) {

        int n;

        cin >> n;

        vector<int> v(n);

        

        // Read the vector elements

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

            cin >> v[i];

        }

        

        // Process the vector

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

            int min_val = min(v[i], v[i + 1]);

            v[i] -= min_val;

            v[i + 1] -= min_val;

        }

        

        // Check if all elements are 0

        bool f = true;

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

            if (v[i] != 0) {

                f = false;

                break;

            }

        }

        

        // Output the result

        if (!f) {

            cout << "NO" << endl;

        } else {

            cout << "YES" << endl;

        }

    }

    

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include<bits/stdc++.h>
using namespace std;

#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ll long long
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vl vector<long long>
#define vpl vector<pair<long long, long long>>


void solve(){
	ll n;
	cin >> n;
	vl a(n);

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

	bool flag = false;
	for(int i = 1; i < n; ++i){
		if(a[i] >= a[i-1]){
		    a[i] -= a[i-1];
		}
		else{
			flag = true;
			break;
		}
	}
	if(flag)
		cout << "NO\n";
	else
		cout << "YES\n";
}

int main(){
	fast_io;
	int t;
	cin >> t;
	while(t--)
		solve();

}


Solution 2 (runtime: 343 ms):
//i don't know why I am coding in my phone

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main() {

int t;

cin>>t;

while(t--){

ll n,f=0;

cin>>n;

ll arr[n];

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

cin>>arr[i];}

ll x = arr[1]-arr[0];

if(x<0){

f=1;

}

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

x=arr[i]-x;

if(x<0){

f=1;

break;}}

if(f==0){

cout<<"YES"<<endl;}

else {

cout<<"NO"<<endl;}



}





return 0;}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 202 ms):
#include <bits/stdc++.h>
using namespace std;

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

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

        vector<int> brr = arr;
        sort(brr.begin() + l, brr.end());

        vector<int> crr = arr;
        sort(crr.begin(), crr.begin() + r);
        reverse(crr.begin(), crr.begin() + r);

        long long sum_brr = 0, sum_crr = 0;
        for (int i = l; i < r; i++) {
            sum_brr += brr[i];
            sum_crr += crr[i];
        }

        cout << min(sum_brr, sum_crr) << endl;
    }

    return 0;
}


Solution 2 (runtime: 78 ms):
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N=1e5+10;

int n,l,r;

int cnt1,cnt2;

int a[N],b[N];

void solve(){

    cin>>n>>l>>r;

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

        cin>>a[i];

        b[i]=a[i];

    }

    sort(a,a+r);

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

    cnt1=0;cnt2=0;

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

        cnt1+=a[i];

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

        cnt2+=b[i];

    cout<<min(cnt1,cnt2)<<"\n";

}

signed main(){

    ios::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    int T;

    cin>>T;

    while(T--) solve();

    return 0;

}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
#define ll long long int
#define MOD (ll)1000000007
#define dbg(x) cout<< #x <<" : "<<(x)<<endl;
#define dbgv(v) cout<< #v <<" : ";for(auto &i:v) cout<<(i)<<" ";cout<<endl;
using namespace std;
void recur();
void solve(){   
    ll n,l,r;cin>>n>>l>>r;
    vector<ll> a(n);
    for(ll& i:a)cin>>i;
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.begin()+r,a.end());
    ll p1=0,suml=0,sumr=0,p2=r-1,c=0;
    while(p1<l-1 && p2>l-2 && c<r-l+1){
        c++;
        if(a[p1]<a[p2]){
            suml+=a[p1];
            p2--;
            p1++;
        }
        else if(a[p1]>a[p2]){
            break;
        }
    }
    while(p2>l-2){
        suml+=a[p2];
        p2--;
    }
    p1=r;p2=r-1;c=0;
    while(p1<n && p2>l-2 && c<r-l+1){
        c++;
        if(a[p1]<a[p2]){
            sumr+=a[p1];
            p2--;
            p1++;
        }
        else if(a[p1]>a[p2]){
            break;
        }
    }
    while(p2>l-2){
        sumr+=a[p2];
        p2--;
    }
    cout<<min(sumr,suml)<<endl;
}

























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

Solution 2 (runtime: 187 ms):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll solve(ll n, ll l,ll r, vector<ll>&a){

   l--; r--;

   vector<ll>x,y;

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

     x.push_back(a[i]);

   }

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

      y.push_back(a[i]);

   }

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

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

   ll s1=0,s2=0;

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

     s1+=x[i];

     s2+=y[i];

   }

   return min(s1,s2);

}

int main() {

    int t;

    cin >> t;

    while (t--) {

       ll n,l,r;

       cin>>n>>l>>r;

       vector<ll>a(n);

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

          cin>>a[i];

          

      ll ans = solve(n,l,r,a);

      cout<<ans<<endl;

    }

    return 0;

}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <iostream>
#include <string>
using namespace std;
#include <iomanip>
#include <math.h>
typedef long long ll;
int main() {
	ll t; cin >> t;
	while (t--) {
		ll n; cin >> n;
		ll* arr = new ll[n];
		ll max = 0;
		for (ll i = 0; i < n; i++) {
			cin >> arr[i]; max += arr[i];
		}
		
		for (ll i = n-1; i >0; i--) {
			if (max < arr[i] - arr[0]) {
				max = arr[i] - arr[0];
			}
			if (max < -arr[i] + arr[0]) {
				max = -arr[i] + arr[0];
			}
			for (ll j = 0; j < i; j++) {
				arr[j] = arr[j + 1] - arr[j];
			}
		}
		cout << max << endl;
	}
}

Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ll t;
    cin >> t;

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

        vector<ll> a(n);
        ll sum = 0;
        for(ll i=0; i<n; i++) cin >> a[i], sum += a[i];

        ll maxi = sum;
        while(a.size() > 1){
            ll last = a.size()-1;
            ll sum = 0;
            if(a[last]-a[0] >= 0){
                for(ll i=0; i<a.size()-1; i++){
                    a[i] = a[i+1]-a[i];
                    sum += a[i];
                }
                maxi = max(maxi, sum);
                a.pop_back();
            } 
            else{
                reverse(a.begin(), a.end());
            }
        }
        maxi = max(a[0], maxi);

        cout << maxi << endl;
    }
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>
using namespace std;

string toBinary(long long num) {
    bitset<64> bin(num); 
    return bin.to_string();  
}

long long toLongLong(const string& bin) {
    bitset<64> b(bin);
    return b.to_ullong();  
}

void solve() {
    int l, r;
    cin >> l >> r;
    
    string s1 = toBinary(l);
    string s2 = toBinary(r);
    
    string s3 = "", s4 = "", s5 = "";
    int cnt = 0;
    if(l==r){
        cout<<0
<<endl;    }
    
    for (int i = 0; i < 64; i++) {
        if (s1[i] == s2[i]) {
           
            s4 += s1[i];
            s5 += s2[i];
            cnt++;
        } else {
            break;
        }
    }

    
     
     s4+='1';
     s5+='0';
     

    for (int i = cnt+1; i < 64; i++) {
        
        s4 += '0';
        s5 += '1';
    }
     

    s4[s4.length()-1]='0';
    long long res2 = toLongLong(s4);
    long long res3 = toLongLong(s5);
    s3=s5;
    s3[s3.length()-1]='0';
    long long res1 = toLongLong(s3);
    if(res1<l){
        res1=r;
    }
     if(res1==0 && l>0){
        res1=r;
    }

    cout << res1 << " " << res2 << " " << res3 << endl;

   
}

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


Solution 2 (runtime: 46 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define  nl "\n"
#define f first
#define s second
const int mod = 1e9 + 7, inf = 1e18;
#define all(a) a.begin(),a.end()
#define forn(a, c) for (int (a) = 0; (a) < (c); (a)++) 
#define cin(v) for(auto &i:v){cin>>i;}
#define cout(v) for(auto &i:v){cout<<i<<" ";}cout<<nl;
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); pri(_it, args); cout<<nl;}
void pri(istream_iterator<string> it) {}
template<typename T, typename... Args>
void pri(istream_iterator<string> it, T a, Args... args) {cout << *it << "  " << a << nl;pri(++it, args...);}
string dtobin(int n,int k = 0){string res = "";while(n>0 || k>0){k--;if(n&1){res += "1";}else{res += "0";}n>>= 1;}reverse(res.begin(),res.end());return res;}
int binexp(int a,int b){if(b==0){return 1;}int res = 1;while(b){if(b&1){res*= a;}b>>=1; a *= a;}return res;}
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int l, r;cin>>l>>r;
    int x, y, z;
    x = l;
    for(int i = 0; i < 30;i++)
    {
        if((x | (1 << i ) )< r)x |= (1 << i);
    }
    y = x + 1;
    z = (x == l ? r : l);
    cout<<x<<" "<<y<<" "<<z<<nl;
}
signed main()
{
    (ios_base::sync_with_stdio(false), cin.tie(NULL));
    auto begin = std::chrono::high_resolution_clock::now();
    int t;t = 1;
    cin>>t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 218 ms):
// Online C++ compiler to run C++ program online
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--){
        int n , k;
        cin >> n >> k;
        vector<int> nums(n);
        for(int i = 0; i < n; i++) cin >> nums[i];
        map<int,int> mp;
        for(auto num : nums) mp[num]++;
        vector<int> vec;
        for(auto [x, y] : mp) vec.push_back(y);
        sort(vec.begin(), vec.end());
        int i = 0;
        int sum = 0;
        while(i < vec.size() && (sum + vec[i] <= k) ){
            sum += vec[i];
            i++;
        }
        if(i >= vec.size()) cout << 1 << endl;
        else cout << (vec.size() - i) << endl;
        
    }

    return 0;
}

Solution 2 (runtime: 186 ms):
#include <iostream>
#include <algorithm>
using namespace std;
int n,k;
const int N=1e5+10;
int a[N];
struct py{
	int v;
	int num;
};
bool cmp(py a,py b)
{
	return a.num<b.num;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		sort(a,a+n);
		py b[n];
		int g=0;
		b[0].v=a[0];
		b[0].num=1;
		for(int i=1;i<n;i++)
		{
			if(a[i]!=b[g].v)
			{
				g++;
				b[g].v=a[i];
				b[g].num=1;
			}
			else
			{
				b[g].num++;
			}
		}
		int ans=g+1;
		sort(b,b+g+1,cmp);
		int i=0;
		while(k>=0)
		{
			if(b[i].num>k)
			break;
			else
			{
				k-=b[i].num;
				ans--;
				i++;
			}
			
		}
		if(ans<=0)
		cout<<1<<endl;
		else
		cout<<ans<<endl;
	}
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 187 ms):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

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

Solution 2 (runtime: 186 ms):
#include<bits/stdc++.h>

using namespace std;

long long mod=998244353;

int main(){

    int t;

    cin>>t;

    while(t--){

        int n,l,r;

        cin>>n>>l>>r;

        int a[n];

        vector<int> b;

        long long ans1=0;

        long long ans2=0;

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

        cin>>a[i];

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

        b.push_back(a[i]);

        sort(a,a+r);

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

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

        ans1+=a[i];

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

        ans2+=b[i];

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

    }

}
Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 374 ms):
#include <cstdio>
#include <set>

int main(){

    long t; scanf("%ld", &t);
    while(t--){
        long n, m; scanf("%ld %ld", &n, &m);
        std::multiset<long, std::greater<long> > msa, msb;
        long long diff(0);

        for(long p = 0; p < n; p++){
            long x; scanf("%ld", &x);
            msa.insert(x);
            diff += x;
        }

        for(long p = 0; p < m; p++){
            long x; scanf("%ld", &x);
            if(msa.count(x)){msa.erase(msa.find(x));}
            else{msb.insert(x);}
            diff -= x;
        }

        if(diff){puts("No"); continue;}

        while(!msb.empty() && !msa.empty() && msa.size() >= msb.size() && *msb.begin() >= *msa.begin()){
            long x = *msb.begin();
            long y = *msa.begin();
            msb.erase(msb.begin());
            if(x == *msa.begin()){msa.erase(msa.begin());}
            else{msb.insert(x / 2); msb.insert((x + 1) / 2);}
        }

        puts(msa.empty() && msb.empty() ? "Yes" : "No");
    }

}

Solution 2 (runtime: 405 ms):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <ctime>
#include <list>
#include <stack>
#include <bitset>
#include <chrono>
#define N 200005
#define M 1005
#define eps 1e-7
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
const int mod2 = 998244353;
const int inf = 1000000010;
//const __int128 hmod = 212370440130137957LL;
//srand((unsigned)time(0)); 
const int base=337;
int n, m, q;
int k;
int t;
int a[N];
int b[N];
int main() {
  cin>>t;
  for (int cas=1; cas<=t; ++cas) {
    scanf("%d%d", &n, &m);
    map<int,int> ca, cb;
    ca.clear();
    cb.clear();
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      ca[a[i]]++;
    }
    for (int i = 1; i <= m; ++i) {
      scanf("%d", &b[i]);
      cb[b[i]]++;
    }
    bool ok = true;
    int tot=m;
    while(!cb.empty()) {
      auto x = (*cb.rbegin()).first;
      if (ca.empty()) {
        ok=false;
        break;
      }
      if (tot > n) {
        ok=false;
        break;
      }
      // cout<<"x="<<x<<endl;
      // if (x==1) {
      //   cout<<(int)ca.size()<<endl;
      //   if (ca.find(x) != ca.end()) cout<<"v="<<ca[x]<<endl;
      // }
      if (x < (*ca.rbegin()).first) {
        ok=false;
        break;
      }
      if (ca.find(x) != ca.end() && ca[x]) {
        cb[x]--;
        ca[x]--;
        if (ca[x] == 0) ca.erase(x);
        if (cb[x] == 0) cb.erase(x);
        tot--;
      } else {
        cb[x]--;
        if (cb[x] == 0) cb.erase(x);
        tot--;
        if (x%2) {
          cb[x/2]++;
          cb[x/2+1]++;
        } else {
          cb[x/2]+=2;
        }
        tot += 2;
      }
    }
    if (!ca.empty() || !ok) puts("No");
    else puts("Yes");
  }

  // cin>>n;
  // srand((unsigned)time(0)); 
  // for (int i = 1; i <= n; ++i) {
  //   printf("%d ", rand()%n+1);
  // }
  // cout<<endl;
  return 0;
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 46 ms):
#include<bits/stdc++.h>
using namespace std;
int n;
int t;
long long a[51];
long long x[51];
long long b;
long long ans;
void m(long long e[]){
	if(e[1]>e[n]){
		for(int i=1;i<n;i++){
			x[n-i]=a[i+1]-a[i];
		}
	}else{
		for(int i=2;i<=n;i++){
			x[i-1]=a[i]-a[i-1];
		}
	}for(int i=1;i<=n-1;i++)
		a[i]=x[i];
	n--;
}
int main(){
	scanf("%d",&t);
	while(t--){
		b=0;
		ans=-50001;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			b+=a[i];
		}ans=b;
		while(n>1){
			ans=max(ans,abs(a[n]-a[1]));
			m(a);
		}printf("%lld\n",ans);
	}
	return 0;
}

Solution 2 (runtime: 109 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

map<vector<ll>, ll> memo;

ll maxSum(vector<ll> a){
    if(a.size() == 1) return a[0];
    if(memo.find(a) != memo.end()) return memo[a];

    ll sum = a[0];
    vector<ll> b, c;
    for(ll i = 1; i < a.size(); i++){
        sum += a[i]; b.push_back(a[i] - a[i - 1]);
    }

    for(ll i = b.size() - 1; i >= 0; i--) c.push_back(-b[i]);

    memo[a] = max({sum, maxSum(b), maxSum(c)});
    return memo[a];
}

void solve(){
    ll n, tmp;
    cin >> n;

    vector<ll> a;
    for(int i = 0; i < n; i++){
        cin >> tmp; a.push_back(tmp);
    }

    cout << maxSum(a) << endl;
}

int main(){
    int t;
    cin >> t;

    while(t--){
        solve();
    }
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>
#define ll int64_t
#define nl '\n'
#define F first
#define S second
#define pai pair<int, int>
#define int int64_t

using namespace std;

int oz(int a, int b)
{
    return max(a - b, b - a);
}

void solve()
{
    int n;
    cin >> n;
    vector<int> in(n, 0);
    int ans = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> in[i];
        ans += in[i];
    }
    vector<int > vc = in;
    for (int i = 1; i <= n - 1; ++i)
    {
        vector<int> s = vc;
        int sum = vc[vc.size() - 1] - vc[0];
        ans = max({ans, -sum, sum});
        for (int j = 1; j < vc.size(); ++j)
            vc[j] -= s[j - 1];
        vc.erase(vc.begin());
    }
    cout << ans << nl;
}

int32_t main()
{
    cin.tie(0)->sync_with_stdio(0);
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
	int t;
	cin >> t;
	while (t--)
        solve();
    return 0;
}


Solution 2 (runtime: 46 ms):
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<long long> a(n);
        long long ans=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            ans+=a[i];
        }
        while(a.size()>1){
            if(a[0]>a.back()){
                reverse(a.begin(),a.end());
            }
            ans=max(ans,a.back()-a[0]);
            for(int i=a.size()-1;i>0;i--){
                a[i]=a[i]-a[i-1];
            }
            a.erase(a.begin()+0);
        }
        cout<<ans<<endl;
    }
}