ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 93 ms):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int rec(int i, int j,  string &a,string &b,string &c, vector<vector<int>>&dp){
    
 
    if (i+j>=c.size())
    {
        return 0;
    }
    if (dp[i][j]!=-1)
    {
    return dp[i][j];
    }

        int take1=1e9;
        int take2=1e9;
        if (i<a.size())
        {
            take1=rec(i+1,j,a,b,c,dp);
            if( a[i]!=c[i+j])
              {
                take1++;
              }
        }
        if (j<b.size())
        {
            take2=rec(i,j+1,a,b,c,dp);
            if( b[j]!=c[i+j])
              {
                take2++;
              }
        }
        
         return dp[i][j]=min(take1,take2);
}

int main() {
    // Write C++ code here
  int t;
  cin>>t;
  while(t--)
  {

      string a,b,c;
      cin>>a>>b>>c;
      int n=a.size();
      int m=b.size();
      vector<vector<int>>dp(n+1,vector<int>(m+1,-1));
      
   cout<<rec(0,0,a,b,c,dp)<<endl;
  }
    return 0;
}

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

using namespace std;



const int MAX_N = 1001;

int dp[MAX_N][MAX_N];



int findMinDeleted(string &a, string &b, string &c, int n, int m, int k)

{

  if (k == 0)

    return 0;



  if (dp[n][m] != -1)

    return dp[n][m]; // Check memoized result



  if (n == 0)

  {

    int count = 0;

    for (int i = m - 1; i >= 0; i--, k--)

    {

      if (c[k - 1] != b[i])

        count++;

    }

    return dp[n][m] = count;

  }



  if (m == 0)

  {

    int count = 0;

    for (int i = n - 1; i >= 0; i--, k--)

    {

      if (c[k - 1] != a[i])

        count++;

    }

    return dp[n][m] = count;

  }



  int res = INT_MAX;



  if (c[k - 1] == a[n - 1] && c[k - 1] == b[m - 1])

  {

    res = min(findMinDeleted(a, b, c, n - 1, m, k - 1),

              findMinDeleted(a, b, c, n, m - 1, k - 1));

  }

  else if (c[k - 1] == a[n - 1])

  {

    res = min(findMinDeleted(a, b, c, n - 1, m, k - 1),

              1 + findMinDeleted(a, b, c, n, m - 1, k - 1));

  }

  else if (c[k - 1] == b[m - 1])

  {

    res = min(findMinDeleted(a, b, c, n, m - 1, k - 1),

              1 + findMinDeleted(a, b, c, n - 1, m, k - 1));

  }

  else

  {

    res = 1 + min(findMinDeleted(a, b, c, n - 1, m, k - 1),

                  findMinDeleted(a, b, c, n, m - 1, k - 1));

  }



  return dp[n][m] = res; // Store result before returning

}



void solve()

{

  int t;

  cin >> t;

  while (t--)

  {

    string a, b, c;

    cin >> a >> b >> c;

    int n = a.size(), m = b.size(), k = c.size();



    memset(dp, -1, sizeof(dp)); // Reset DP array before each test case

    int ans = findMinDeleted(a, b, c, n, m, k);

    cout << ans << endl;

  }

}



int main()

{

  ios_base::sync_with_stdio(false);

  cin.tie(NULL);

  solve();

  return 0;

}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 78 ms):
#include<bits/stdc++.h>
using namespace std;
void solve (string s){
	
	int m=s.size();
	bool flag =  true;
	while(flag){
		flag =false;
		for(int i=1; i<m; i++){
			
		if(s[i]-1>0 && s[i-1]<s[i]-1){
		    int k=s[i];
		    s[i]=s[i-1];
		    s[i-1]=k-1;
		    flag=true;
		}
			
		
	}
	}
	
	
	
	
	cout<<s;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		solve(s);
		cout<<endl;
	}
}

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

using namespace std;

int t;

string s;

int main(){

	cin>>t;

	while(t--){

		cin>>s;

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

			int ans1=0,ans2=s[i]-'0';

			for(int j=1;j<=9&&i+j<s.length();j++){

				if(s[i+j]-'0'-j>ans2){

					ans2=s[i+j]-'0'-j;

					ans1=j;

				}

			}

			s[i+ans1]-=ans1;

			int tmp=s[i+ans1];

			for(int j=i+ans1;j>=i+1;j--){

				s[j]=s[j-1];

			}

			s[i]=tmp;

		}

		cout<<s<<endl;

	}

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

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

#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O2")
using ll = long long;
const ll MAXN = 2e5 + 5;
const ll INF = 2e9;
const ll MOD = 1e9 + 7;
const ll LOG = 30;
#define fi first
#define se second
#define pll pair <ll, ll>
#define vll vector <ll>
#define endl '\n'
#define debug(x) cout << #x << ": " << (x) << endl;

ll n, m;
map <ll, ll> mp;
ll a [MAXN];

bool f(ll x){
    if(mp[x]){
        mp[x]--;
        return 1;
    }
    if(x == 1) return 0;
    if(x & 1) return f((x/2)+1) && f(x/2);
    return f(x/2) && f(x/2);
}

void solve(){
    mp.clear();
    cin >> n >> m;
    for(ll i = 1; i <= n; i++){
        ll x; cin >> x;
        mp[x]++;
    }
    for(ll i = 1; i <= m; i++){
        cin >> a[i];
        a[i] *= -1;
    }
    sort(a+1, a+m+1);
    for(ll i = 1; i <= m; i++){
        a[i] *= -1;
        if(!f(a[i])){
            cout << "No" << endl;
            return;
        }
    }
    bool ya = 1;
    for(auto [x, fk] : mp) if(fk) ya = 0;
    if(ya) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

Solution 2 (runtime: 343 ms):
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define db double
#define P point
#define Fr(i,a,b) for(int i=a;i<=b;i++)
#define Fr_(i,a) for(auto i:a)
#define pb push_back
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int> 
#define fir first
#define sec second
#define pq priority_queue
#define mkp make_pair
#define memset(a,b) memset(a,b,sizeof(a))
#define new_opt friend bool operator
#define endl '\n'
#define dbug cout<<"Ciallo~(<'w< )~"<<endl

using namespace std;

const int N=2e5+10;
const int N_=6e3+10;
const int M=998244353;
const int inf=2e9;

int fast_read=1 ;
int multiple_test=1 ;
int n,m,k;

map<int,int> mp;

bool dfs(int x)
{
    if(mp[x]) 
    {
        mp[x]--;
        return true;
    }
    if(x==0) return false;
    if(x%2==0) return dfs(x/2)&&dfs(x/2);
    else 
    {
        if(x==1) return false;
        return dfs(x/2+1)&&dfs(x/2);
    }
}

int a[N],b[N];
void slv()
{
    cin>>n>>m;
    mp.clear();
    Fr(i,1,n)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    Fr(i,1,m) cin>>b[i];
    int suma=0,sumb=0;
    Fr(i,1,n) suma+=a[i];
    Fr(i,1,m) sumb+=b[i];
    if(suma!=sumb)
    {
        cout<<"NO"<<endl;
        return ;
    }
    Fr(i,1,m)
    {
        if(!dfs(b[i]))
        {
            cout<<"NO"<<endl;
            return ;
        }
    }
    cout<<"YES"<<endl;
}

signed main()
{
    if(fast_read) fastio;
    int mt=1;
    if(multiple_test) cin>>mt;
    while(mt--)
    {
        slv();
    }
}
Problem: 2062C - Cirno and Operations
Language: c++17 

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

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;

ll solve(vector<ll> &nums) {
    ll sum = 0;
    for (ll i = 0; i < nums.size(); i++) {
        sum += nums[i];
    }
    return sum;
}

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

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

        ll ans = LLONG_MIN;
        queue<vector<ll>> q;
        set<vector<ll>> visited; 

        q.push(v);
        visited.insert(v);
        
        reverse(v.begin(),v.end());
        
        if(!visited.count(v)){
        	q.push(v);
        	visited.insert(v);
		}

        while (!q.empty()) {
            vector<ll> temp = q.front();
            q.pop();

            ans = max(ans, solve(temp));

            if (temp.size() <= 1) 
                continue;

            vector<ll> temp1;
            for (ll i = 0; i < temp.size() - 1; i++) {
                temp1.push_back(temp[i + 1] - temp[i]);
            }

            if (!visited.count(temp1)) {
                q.push(temp1);
                visited.insert(temp1);
            }

            reverse(temp1.begin(), temp1.end());

            if (!visited.count(temp1)) {
                q.push(temp1);
                visited.insert(temp1);
            }
        }

        cout << ans << endl;
    }
    return 0;
}

Solution 2 (runtime: 62 ms):
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <unordered_map>
#include <limits.h>
#include <set>
#include <array>
#define int long long

using namespace std;
using aii =array<int,3>;

typedef pair<int,int> PII;
typedef pair<char,int> PCI;
typedef pair<string,aii> PSII;
#define fi first
#define se second
constexpr int N = 1e6+5,mod=1e9+7;

void solve()
{
 int t;cin>>t;
 while(t--)
 {
    int n;cin>>n;
    vector<int> v;
    int ma=0;
    for(int i=0;i<n;i++)
    {
        int x;cin>>x;
        v.push_back(x);
        ma+=x;
    }
     if(n==1)
     {
         cout<<ma<<endl;
         continue;
     }
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        vector<int> a;
        int sz=v.size();
        for(int j=0;j+1<sz;j++)
        {
            a.push_back(v[j+1]-v[j]);
            sum+=v[j+1]-v[j];
        }
        v=a;
        ma=max(ma,abs(sum));
    }
    cout<<ma<<endl;
 }
}

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


/**
*　　┏┓　　　┏┓+ +
*　┏┛┻━━━┛┻┓ + +
*　┃　　　　　　　┃
*　┃　　　━　　　┃ ++ + + +
*  ████━████+
*  ◥██◤　◥██◤ +
*　┃　　　┻　　　┃
*　┃　　　　　　　┃ + +
*　┗━┓　　　┏━┛
*　　　┃　　　┃ + + + +Code is far away from 　
*　　　┃　　　┃ + bug with the animal protecting
*　　　┃　 　 ┗━━━┓ 神兽保佑,代码无bug　
*　　　┃ 　　　　　　 ┣┓
*　　  ┃ 　　　　　 　┏┛
*　    ┗┓┓┏━┳┓┏┛ + + + +
*　　　　┃┫┫　┃┫┫
*　　　　┗┻┛　┗┻┛+ + + +
*/
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 343 ms):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>

using namespace std;

int t, n;
int a[200005];

int main() {
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }

        if (a[1] > a[2]) {
            cout << "NO" << endl;
            continue;
        }
        
        bool valid = true;
        for (int i = 2; i < n; i++) {
            a[i] -= a[i-1];
            a[i-1] = 0;
            if (a[i] > a[i+1]) {
                valid = false;
                break;
                
            }
        }
        
        for (int i = 1; i < n; i++) {
            if (a[i] > a[i+1]) {
                valid = false;
                break;
            }
        }
        
        cout << (valid ? "YES" : "NO") << endl;
    }
}

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

#include <algorithm>

#include <map>

#include <vector>

using namespace std;

bool check(vector<int>&vec){

int q=0;

int k = vec[0];

for(int i=0;i<vec.size()-1;i++){

int p = min(k,vec[i+1]);

int a = k-p;

int b = vec[i+1]-p;

if(a>b){

q++;

cout<<"NO\n";

break;

}

k=b;

}

if(q==0){

cout<<"YES\n";

}

}

int main(){

int n;

cin>>n;

while(n--){

int p;

cin>>p;

vector<int>vec(p,0);

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

cin>>vec[i];

}

check(vec);

}

}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 62 ms):
// JIANG KE XUAN
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define inf 1e18
#define endl "\n"
#define PII pair<int,int>
#define fi first
#define se second
#define vi vector<int>
#define vc vector<char>
using namespace std;
const int mod = 998244353;
const int maxn = 5005;

int qui(int n, int k) {
	int res = 1;
	while (k) {
		if (k & 1) {
			res = res * n;
		}
		n = n * n;
		k >>= 1;
	}
	return res ;
}

int highestBit(int num) {
    if (num == 0) return 0;
 
    int pos = 0;
    while (num > 1) {
        num >>= 1;
        pos++;
    }
    return pos;
}

void solve(){
	int l ,r ;
	cin >> l >> r ;
	
	int k = 0 ;
	for(int i = 30 ;i>=0 ;i--){
	 if (((l >> i) & 1) ^ (((r >> i) & 1))) {
	   k = i;
	   break;
	  }
	}
	
	int x = ((1 << k) - 1 ) | l ;
	int y = x + 1 ;
	
	int z = (x == l ? r : l);
	
	cout << x << " " << y <<" " << z << "\n";
}

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

Solution 2 (runtime: 93 ms):
#include <bits/stdc++.h>
#define OPEN_DEBUG 1
#define DEBUG if(OPEN_DEBUG)
using namespace std;typedef unsigned long long ULL; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL;
const int MOD = 1e9+7; const double EPI=1e-6; const int INF=0x3f3f3f3f; const LL LLINF=1e17;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; //RDLU
void YES() {cout<<"YES\n";} void NO(){cout<<"NO\n";} void Yes() {cout<<"Yes\n";} void No(){cout<<"No\n";} void endl(){cout << "\n";}
struct Edge {int x, y, z;};
LL qpow(LL a, LL b, LL p) { LL ans = 1; a = a % p; while(b) {if (b & 1) ans = (ans * a) % p; a = a * a % p; b >>= 1;} return ans;}
LL gcd(LL a, LL b) {if (!b) return a; return gcd(b, a % b);}
LL exgcd(LL a, LL b, LL& x, LL& y) {if (!b) {x = 1, y = 0; return a;} LL xx, yy; LL d = exgcd(b, a % b, xx, yy);x = yy;y = xx - a / b * yy;return d;}

bool MULTI_TESTS = 1;

int solve() {
    int l, r;
    cin >> l >> r;
    int x = 0;
    int j = 30;
    for (j = 30; j >= 0; j--) {
        if ( (l & (1 << j)) != (r & (1 << j))) {
            x |= (1 << j);
            break;
        }
    }
    x |= (l >> j << j) ;
    if (x == r) {
        cout << x - 2 << " " << x - 1 << " " << x << "\n";
    } else {
        cout << x - 1 << " " << x << " " << x + 1 << "\n";
    }

    
   

    return 0;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // cout << fixed << setprecision(2);

    int ttt = 1;
    if (MULTI_TESTS) cin >> ttt;
    for (int i = 1; i <= ttt; i++) {
        solve();
    }


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

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

using namespace std;

void solve() {
    int n, l, r; cin >> n >> l >> r;
    r--;
    l--;
    vector<int> left;
    vector<int> right;
    vector<int> mid;
    for (int i = 0; i < n; ++i) {
        int x; cin >> x;
        if (i < l) left.push_back(x);
        else if (i < r + 1) mid.push_back(x);
        else right.push_back(x);
    }
    sort(left.begin(), left.end());
    sort(right.begin(), right.end());
    sort(mid.begin(), mid.end());
    vector<long long> prefleft(left.size() + 1);
    vector<long long> prefright(right.size() + 1);
    for (int i = 1; i < left.size() + 1; ++i) prefleft[i] = prefleft[i - 1] + left[i - 1];
    for (int i = 1; i < right.size() + 1; ++i) prefright[i] = prefright[i - 1] + right[i - 1];
    vector<long long> prefmid(mid.size() + 1);
    for (int i = 1; i < mid.size() + 1; ++i) prefmid[i] = prefmid[i - 1] + mid[i - 1];
    long long mn = (1LL << 50LL);
    for (int x = 0; x < r - l + 2; ++x) {
        if (r - l + 1 - x < prefleft.size()) {
            mn = min(mn, prefmid[x] + prefleft[r - l + 1 - x]);
        }
        if (r - l + 1 - x < prefright.size()) {
            mn = min(mn, prefmid[x] + prefright[r - l + 1 - x]);
        }
    }
    cout << mn << endl;
}

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


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

void solve(){
    // a1 a2 a3 ... ai-1 ai ai+1  ... an
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    vector<int> left,right;
    for(int i=0;i<r;i++){
        left.push_back(a[i]);
    }
    for(int i=l-1;i<n;i++){
        right.push_back(a[i]);
    }
    sort(left.begin(),left.end());
    sort(right.begin(),right.end());
    long long ansl=0,ansr=0;
    if(left.size()==0) ansl=INFINITY;
    if(right.size()==0) ansr=INFINITY;
    for(int i=0;i<r-l+1;i++){
        if(left.size()>0)  ansl+=left[i];
        if(right.size()>0) ansr+=right[i];
    }
    cout<<min(ansl,ansr)<<endl;
}

int main(){
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

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

using namespace std;

const int N = 1e5 + 10;
int a[N];
int cnt[N], indx;
int n, m;

int main(){
	int _;
	scanf("%d", &_);
	while(_ --){
		memset(cnt, 0, sizeof cnt);
		indx = 0;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i ++)
			scanf("%d", a + i);
		sort(a + 1, a + n + 1);
		a[n + 1] = -1;
		int l = 1;
		for(int i = 2; i <= n + 1; i ++){
			if(a[l] != a[i]){
				cnt[++ indx] = i - l;
				l = i;
			}
		}
		sort(cnt + 1, cnt + 1 + indx);
		//for(int i = 1; i <= indx; i ++)
		//	printf("cnt[%d], %d\n",i, cnt[i]);
		int t = indx, i = 1;
		while(m > 0 and i <= indx){
			if(m >= cnt[i]){
				t --;
			}
			m -= cnt[i];
			i ++;
		}
		if(t == 0)
			t = 1;
		printf("%d\n", t);
	}
	return 0;
}


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

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

    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        map<int, int> freq;

        // Count occurrences
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            freq[temp]++;
        }

        // Extract frequencies and sort them
        vector<int> counts;
        for (auto &[num, count] : freq) {
            counts.push_back(count);
        }
        sort(counts.begin(), counts.end());

        int ans = counts.size();
        int i = 0;

        // Greedily remove smallest frequency elements
        while (i < counts.size() && k >= counts[i]) {
            k -= counts[i];
            ans--;
            i++;
        }

        cout << max(1, ans) << "\n";
    }
    return 0;
}
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;

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

    ll t;
    cin>>t;
    //t=1;

    while(t--){
        string ch;
        cin>>ch;
        ll two=0,three=0,sum=0;
        for(ll i=0;i<ch.size();i++){
            if((ch[i]-'0')==2)two++;
            else if((ch[i]-'0')==3)three++;
            sum+=(ch[i]-'0');
        }
        ll req=9-sum%9;
        if(req==9)cout<<"YES";
        else{
            bool check=false;
            for(ll i=0;i<=min(two,50LL);i++){
                for(ll j=0;j<=min(50LL,three);j++){
                    if((2*i+6*j+sum)%9==0){
                        check=true;
                        break;
                    }

                }
            }
            if(check)cout<<"YES";
            else cout<<"NO";
        }

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


Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		int sum=0;
		int to=0;
		int th=0;
		for(int i=0;i<s.size();i++){
			sum+=(s[i]-'0');
			if(s[i]=='2')to++;
			if(s[i]=='3')th++;
		}
		int p=9-(sum%9);
		if(p==9){cout<<"YES\n";
		continue;}

		if(p==2&&to>0)cout<<"YES\n";
		else if(p==1&&(to>=5 || (to>=2 && th>=1)))cout<<"YES\n";
		else if(p==4&&to>1)cout<<"YES\n";
		else if((p==6)&&(to>2||th>0))cout<<"YES\n";
		else if(p==3&&(to>=6 || th>=2 || (th>=1 && to>=3)))cout<<"YES\n";
		else if(p==8&&(to>3|(to>0&&th>0)))cout<<"YES\n";
		else if(p==7&&(to>=8 || (to>=2 && th>=2) || (to>=5 && th>=1))){
			cout<<"YES\n";
		}
		else if(p==5&&(to>=7 || (to>=1 && th>=2) || (to>=4 && th>=1)) ){
           cout<<"YES\n";
		}
        else cout<<"NO\n";
 
 
	}
	
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

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

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

        for(long long i =0 ; i<n-1 ;i++){
            long long temp = min(v[i] , v[i+1]);
            v[i] =v[i] - temp;
            v[i+1] = v[i+1]- temp;
        }
        v[n-1]=0;

        long long cnt =0;
        for(long long i=0 ;i< n ;i++){
            if(v[i]!=0){
                ++cnt;
            }
        }

        if(cnt == 0){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    return 0;
}

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

/*.......................code by LegendARY...............................*/

using namespace std;

#define ll long long

//cout<<fixed<<setprecision(6) <<vv;

// #define bug cout << "Thara hi dosh manan ja ga \n";

// #define bug1 cout << "Thara hi dosh manan ja ga 1\n";

using vb = vector<bool>;

#define nl cout<<"\n";

using vvb = vector<vb>;

using vi = vector<int>;

using vl = vector<ll>;

using vll = vector<vl>;

using vs = vector<string>;

#define put emplace_back;

bool _isSorted(vl &arr){

for(ll i =0;i<arr.size()-1;i++){

if(arr[i] > arr[i+1]){

return false;

}



}

return true;

}

void MYFUN(){

ll n;

cin>>n;

vector<ll>arr(n);

for(auto &it:arr)

cin>>it;

ll maxi = LLONG_MAX;

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

ll sub = min(arr[i],arr[i+1]);

arr[i] -= sub;

arr[i+1] -= sub;

}

if(_isSorted(arr)){

cout<<"YES\n";

}

else cout<<"NO\n";

}

int main(){

ios_base::sync_with_stdio(false);

cin.tie(NULL);

cout.tie(NULL);

ll n;

cin>>n;

while(n--)MYFUN();

}