/*****************************************
备注：
******************************************/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int ll
const int MAXN = 2e6 + 10;
const int MR = 10 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int _ = 0;
const bool debug = false;
#define check(x)cout << #x << "   ===   " << x << endl;

inline int read()
{
    int s = 0, f = 1;
    char x = getchar();
    while(x < '0' || x > '9')
    {
        if(x == '-')f = -1;
        x = getchar();
    }
    while(x >= '0' && x <= '9')
    {
        s = s * 10 + x - '0';
        x = getchar();
    }
    return s * f;
}

void print(int x)
{
    printf("%d", &x);
}
int n, a[MAXN];
void solve()
{
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> a[i];
	for(int i = 1; i < n; i++)
	{
		int x = min(a[i], a[i + 1]);
		a[i] -= x, a[i + 1] -= x;
	}
	for(int i = 1; i <= n; i++)cout << a[i] << ' ';
	puts("");
	bool flag = true;
	for(int i = n; i >= 2; i--)
	{
		if(a[i] < a[i - 1])flag = false;
	}
	if(flag)puts("YES");
	else puts("NO");
}
signed main()
{
	// int start = clock();
	int T;cin >> T;
	while(T--)solve();
	// printf("%.3lf", clock() - start);
    return ~~(0^_^0);
}

/*****************************************
备注：
******************************************/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int ll
const int MAXN = 2e6 + 10;
const int MR = 10 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int _ = 0;
const bool debug = false;
#define check(x)cout << #x << "   ===   " << x << endl;

inline int read()
{
    int s = 0, f = 1;
    char x = getchar();
    while(x < '0' || x > '9')
    {
        if(x == '-')f = -1;
        x = getchar();
    }
    while(x >= '0' && x <= '9')
    {
        s = s * 10 + x - '0';
        x = getchar();
    }
    return s * f;
}

void print(int x)
{
    printf("%d", &x);
}
int n, a[MAXN];
void solve()
{
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> a[i];
	for(int i = 1; i < n; i++)
	{
		int x = min(a[i], a[i + 1]);
		a[i] -= x, a[i + 1] -= x;
	}
	bool flag = true;
	for(int i = n; i >= 2; i--)
	{
		if(a[i] < a[i - 1])flag = false;
	}
	if(flag)puts("YES");
	else puts("NO");
}
signed main()
{
	// int start = clock();
	int T;cin >> T;
	while(T--)solve();
	// printf("%.3lf", clock() - start);
    return ~~(0^_^0);
}

Answer: 2


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

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

        if (a[0] > a[1])
        {
            cout << "NO" << "\n";
            continue;
        }
        int flag = 0;
        for (int i = 0; i < n; i += 2)
        {
            if (a[i] >= a[i + 1])
            {
                flag = 1;
                break;
            }
        }

        if (flag)
        {
            cout << "NO" << "\n";
        }
        else
        {
            cout << "YES" << "\n";
        }
    }
}

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

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

        int mini;
        for (int i = 1; i < n; i++)
        {
            mini = min(a[i], a[i + 1]);
            a[i] -= mini;
            a[i + 1] -= mini;
        }
        int flag = 0;

        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i + 1])
            {
                flag = 1;
                break;
            }
        }

        if (flag)
        {
            cout << "NO" << "\n";
        }
        else
        {
            cout << "YES" << "\n";
        }
    }
}

Answer: 2


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

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// find_by_order, order_of_key
#define all(v) v.begin(), v.end()
#define ll long long
#define en cout << endl;
#define memo(arr, val) memset(arr, val, sizeof(arr))
#define int long long
#define f first
#define s second
#define sz(s) (int)(s).size()
#define rep(i, st, ed) for (int i = st; i < ed; i++)

bool valid(int i, int j, int n, int m) {
    return i < n && i >= 0 && j < m && j >= 0;
}

int inf = 1e18;

void Sh3lan() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("errors.txt", "w", stderr);

#endif
}

int mod = 1e9 + 7;

int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }

int add(int a, int b) { return ((a % mod) + (b % mod)) % mod; }

int sub(int a, int b) { return ((a % mod) - (b % mod) + mod) % mod; }

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }

int LCM(int a, int b) { return a / GCD(a, b) * b; }

int fast(int a, int m) {
    if (m == 0) return 1;
    int x = fast(a, m / 2);
    x = mul(x, x);
    if (m & 1) x = mul(x, a);
    return x % mod;
}

int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
const int N = 2e3 + 5;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &i: a) cin >> i;
    if (n == 2 && a[0] > a[1]) {
        cout << "NO" << endl;
        return;
    }
    bool ok = false;
    for (int i = 1; i < n - 1; i++) {
        if (a[i] > a[i + 1] && a[i] < a[i - 1]) {
            ok = true;
        }
    }
    if (ok) {
        cout << "NO" << endl;
    } else {
        cout << "YES" << endl;
    }
}

signed main() {
    Sh3lan();
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define ll long long
#define vii vector<int>
#define vib vector<bool>
#define vil vector<ll>
#define vis vector<string>
#define pp pair<int, int>
#define all(d) d.begin(), d.end()
#define py cout << "YES\n"
#define pm cout << "-1\n"
#define pn cout << "NO\n"
#define setit(v, name) ::memset(name, v, sizeof(name))
#define inf 1e5
#define sz(v) ((int)(v.size()))
#define GRAPH vector<vector<char>>
#define ceill(a, b) (((a) + (b) - 1) / b)
#define tests ll t; cin >> t; while (t--)
const double EPS = 1e-9;
ll mod = 1073741824;
const double pi = 3.14159265358979323846;
const int dx8[8] = {1, 0, -1, 0, -1, -1, 1, 1};
const int dy8[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int dr[] = {+0, +0, +1, -1};
int dc[] ={+1, -1, +0, +0};
template<typename T>
istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}

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

    vii arr(n + 1 , 0);

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

    for(int i = 1 ; i < n; i++){
        if(arr[i + 1] < arr[i]){
            int mn = min(arr[i] , arr[i - 1]);
            arr[i] -= mn;
            arr[i - 1] -= mn;

            if(arr[i + 1] <=  arr[i]){
                cout << "NO\n";
                return ;
            }
        }
    }

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

int main(){

    fast
    tests
    solve();
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
void solve(){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=1;i<n;i++){
        int m = min(arr[i],arr[i-1]);
        arr[i]=arr[i]-m;
        arr[i-1] = arr[i-1]-m;
    }
    int b= 0;
    for(int i=0;i<n-1;i++){
        //cout<<arr[i]<<" ";
        if(arr[i]>arr[i+1]){
            b++;
        }
    }

    if(b!=0){
        cout<<"NO"<<endl;
    }else{

        cout<<"YES"<<endl;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std;
void solve(){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    if(arr[0]>=arr[1]){
        cout<<"NO"<<endl;
    }else{

        cout<<"YES"<<endl;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

Answer: 1


#include<iostream>
#include<vector>
#include<climits>

#define vi vector<int>
#define vll vector<ll>
#define ll long long

using namespace std;
char ln = '\n';

void solve()
{
  int n ;cin >> n;
  vi a(n);
  for(int& i :a)
  {
    cin >> i;
  }
  for(int i = 0; i < n-1 ; i++)
  {
    if(a[i] > a[i+1] && i==0)
    {
      cout << "No" <<ln;
      return;
    }
    else
    {
      int minVal = min(a[i],a[i+1]);
      a[i+1] -=minVal;
      a[i] -=minVal;
    }

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

  }
  cout<<ln;
  for(int i = 0 ; i < n-1; i++)
  {
    if(a[i] > a[i+1])
    {
      cout << "No" <<ln;
      return;
    }
  }
  cout << "Yes"<<ln;
}

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

#include<iostream>
#include<vector>
#include<climits>

#define vi vector<int>
#define vll vector<ll>
#define ll long long

using namespace std;
char ln = '\n';

void solve()
{
  int n ;cin >> n;
  vi a(n);
  for(int& i :a)
  {
    cin >> i;
  }
  for(int i = 0; i < n-1 ; i++)
  {
    if(a[i] > a[i+1] && i==0)
    {
      cout << "No" <<ln;
      return;
    }
    else
    {
      int minVal = min(a[i],a[i+1]);
      a[i+1] -=minVal;
      a[i] -=minVal;
    }

  }
  for(int i = 0 ; i < n-1; i++)
  {
    if(a[i] > a[i+1])
    {
      cout << "No" <<ln;
      return;
    }
  }
  cout << "Yes"<<ln;
}

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

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define printarr(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define na cout << "NO" << endl;
#define ha cout << "YES" << endl;

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        vector<int> arr(n);
        for(int i=0; i<n; i++) {
            cin>>arr[i];
        }
        int isnd=1;
        for(int i=1; i<n-1; i++) {
            int minnum=min(arr[i],arr[i-1]);
            arr[i]-=minnum;
            arr[i-1]-=minnum;

            if(arr[i+1]<arr[i] || arr[i+1]<arr[i-1]) {
                isnd=0;
                break;
            }
        }
        if(n==2) {
            if(arr[0]>arr[1]) isnd=0;
        }

        if(isnd==1) ha
        else na

    }
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define printarr(arr, n) for(int i=0; i<n; i++) cout<<arr[i]<<" ";
#define na cout << "NO" << endl;
#define ha cout << "YES" << endl;

int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        vector<int> arr(n);
        for(int i=0; i<n; i++) {
            cin>>arr[i];
        }
        int isnd=1;
        for(int i=1; i<n-1; i++) {
            int minnum=min(arr[i],arr[i-1]);
            if(minnum!=arr[i-1]) {
                isnd=0;
                break;
            }
            arr[i]-=minnum;
            arr[i-1]-=minnum;

            if(arr[i+1]<arr[i] || arr[i+1]<arr[i-1] ) {
                isnd=0;
                break;
            }
        }
        if(n==2) {
            if(arr[0]>arr[1]) isnd=0;
        }

        if(isnd==1) ha
        else na

    }
}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()

{

	int n;cin>>n;

	vector<int> a(n);

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

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

	{

		if(a[i-1]>a[i])

		{

			cout<<"NO"<<endl;

			return ;

		}

		a[i]-=a[i-1];

	}

	cout<<"YES"<<endl;

}

signed main()

{

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cout.tie(nullptr);

	int T;cin>>T;

	while(T--) solve();

	return 0;

}

#include<bits/stdc++.h>

using namespace std;

#define int long long

void solve()

{

	int n;cin>>n;

	vector<int> a(n);

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

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

	{

		if(a[i]>a[i+1]&&a[i+1]>a[i+2])

		{

			cout<<"NO"<<endl;

			return ;

		}

	}

	cout<<"YES"<<endl;

}

signed main()

{

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cout.tie(nullptr);

	int T;cin>>T;

	while(T--) solve();

	return 0;

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;
int t, a[200009], n;

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

    cin >> t;

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

        cout << (is_sorted(a, a + n) ? "YES\n" : "NO\n");
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

    while(t--){
        int n, p = 0;
        int a[n];
        cin >> n;

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

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

        cout << (is_sorted(a, a + n) ? "YES\n" : "NO\n");
    }

    return 0;
}

Answer: 1


// Codeforces Round 998: Problem D

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

#define int long long
#define f first
#define s second

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    if (a[0] > a[1]) {
        cout << "NO\n";
        return;
    }
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1] && (i == 1 || a[i - 2] == 0)) {
            a[i] -= a[i - 1], a[i - 1] = 0;
        }
    }

    // for (auto x : a) cout << x << ' ';
    // cout << '\n';

    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            cout << "NO\n";
            return;
        }
    }

    cout << "YES\n";
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
}

// Codeforces Round 998: Problem D

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

#define int long long
#define f first
#define s second

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    if (a[0] > a[1]) {
        cout << "NO\n";
        return;
    }
    for (int i = 1; i < n; i++) {
        if (a[i] >= a[i - 1] && (i == 1 || a[i - 2] == 0)) {
            a[i] -= a[i - 1], a[i - 1] = 0;
        }
    }

    // for (auto x : a) cout << x << ' ';
    // cout << '\n';

    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            cout << "NO\n";
            return;
        }
    }

    cout << "YES\n";
}

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

void minSub(int *arr, int i)

{

    int j = i;

    while (arr[j]>0 && j>0)

    {

        j--;

    }

    for (; j < i - 1; j++)

    {

        int mi = min(arr[j], arr[j + 1]);

            arr[j] = arr[j] - mi;

            arr[j + 1] = arr[j + 1] - mi;

    }

}

int main()

{

    ios_base::sync_with_stdio(false);

    cout.tie(NULL);

    cin.tie(NULL);

    int t;

    cin >> t;

    while (t--)

    {

        int n, i;

        cin >> n;

        int arr[n];

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

        {

            cin >> arr[j];

        }

            bool isPossible = true;

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

            {

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

                {

                    minSub(arr, i + 1);

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

                    {

                        isPossible = false;

                        break;

                    }

                }

            }

            if (isPossible)

            {

                cout<< "YES"<<endl;

            }

            else

            {

                cout<< "NO"<<endl;

            }

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

void minSub(int *arr, int i)

{

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

    {

        if (arr[j]>0 && arr[j + 1]> 0 )

        {

            int mi = min(arr[j], arr[j + 1]);

            arr[j] = arr[j] - mi;

            arr[j + 1] = arr[j + 1] - mi;

        }

    }

}

int main()

{

    ios_base::sync_with_stdio(false);

    cout.tie(NULL);

    cin.tie(NULL);

    int t;

    cin >> t;

    while (t--)

    {

        int n, i;

        cin >> n;

        int arr[n];

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

        {

            cin >> arr[j];

        }

            bool isPossible = true;

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

            {

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

                {

                    minSub(arr, i + 1);

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

                    {

                        isPossible = false;

                        break;

                    }

                }

            }

            if (isPossible)

            {

                cout<< "YES"<<endl;

            }

            else

            {

                cout<< "NO"<<endl;

            }

    }

    return 0;

}

Answer: 1


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

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

   int tt; cin >> tt;
   while(tt--){
       int n; cin >> n;
       vector<int> a (n);
       for(int i = 0; i < n; i++) cin >> a[i];
       string ans = "YES\n";
       for(int i = 1; i < n; i += 2){
           if(a[i] < a[i - 1]){
               ans = "NO\n";
               break;
           }
       }
       if(n % 2){
           if(a[n - 1] < a[n - 2] - a[n - 3]) ans = "NO\n";
       }
       cout << ans;
   }

    return 0;
}

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

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

   int tt; cin >> tt;
   while(tt--){
       int n; cin >> n;
       vector<int> a (n);
       for(int i = 0; i < n; i++) cin >> a[i];
       for(int i = 1; i < n; i++){
           int mn = min(a[i] , a[i - 1]);
           a[i] -= mn;
           a[i - 1] -= mn;
       }

       if(is_sorted(a.begin() , a.end())) cout << "YES\n";
       else cout << "NO\n";
   }

    return 0;
}

Answer: 2


#include<iostream>

#include<vector>

#include<string>

using namespace std;

int main(){

    int t; cin >> t;

    while(t--){

        int n; cin >> n;

        int arr[n+1]; arr[0]=0;

        string answer = "YES";

        for(int i = 1; i <= n; i++) cin >> arr[i];

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

            if(arr[i] < abs(arr[i-1] - arr[i-2])) {

                answer = "NO"; break;

            } else arr[i-1] = abs(arr[i-1] - arr[i-2]);

        }

        cout << answer << "\n";

    }

}

#include<iostream>

#include<vector>

#include<string>

using namespace std;

int main(){

    int t; cin >> t;

    while(t--){

        int n; cin >> n;

        int arr[n+1]; arr[0]=0;

        string answer = "YES";

        for(int i = 1; i <= n; i++) cin >> arr[i];

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

            if(arr[i] < abs(arr[i-1] - arr[i-2])) {

                arr[i-1] = abs(arr[i-1] - arr[i-2]);

                answer = "NO"; break;

            }

        }

        cout << answer << "\n";

    }

}

Answer: 1


for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    for i in range(n-1):
        temp = min(a[i], a[i+1])
        a[i] -= temp
        a[i+1] -= temp

    possible = True
    for i in range(n-1):
        if a[i] > a[i+1]:
            possible = False
            break

    if possible:
        print("YES")
    else:
        print("NO")

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))

    possible = True
    for i in range(n-1):
        if a[i] - a[i + 1] > 0 and i % 2 == 0:
           possible = False
           break

    if possible:
        print("YES")
    else:
        print("NO")

Answer: 1


#include <bits/stdc++.h>
#define no   "NO"
#define nl   "\n"
#define yes  "YES"
#define pb   push_back
#define ll   long long int
#define all(a)  sort(a.begin(),a.end())
#define allr(a) sort(a.rbegin(),a.rend())
#define rev(a)  reverse(a.begin(),a.end())
#define Code_By_exception ios::sync_with_stdio(false); cin.tie(0);
using namespace std;

void solution()
{
    ll n,tmp;
    cin>>n;
    vector<ll>v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    for(ll i=0;i<n-1;i++){
            tmp=v[i];
        if(v[i]==v[i+1])
            {
                v[i]-=tmp,v[i+1]-=tmp;
                ++i;
            }
        else if(v[i]>v[i+1]){
            cout<<no<<nl;
            return;
        }
        else{
            v[i]-=tmp;
            v[i+1]-=tmp;
        }
    }
    cout<<yes<<nl;
}
int main()
{
    Code_By_exception
    int tc;
    cin>>tc;
    while(tc--)
    {
        solution();
    }
    return 0;
}

#include <bits/stdc++.h>
#define no   "NO"
#define nl   "\n"
#define yes  "YES"
#define pb   push_back
#define ll   long long int
#define all(a)  sort(a.begin(),a.end())
#define allr(a) sort(a.rbegin(),a.rend())
#define rev(a)  reverse(a.begin(),a.end())
#define Code_By_exception ios::sync_with_stdio(false); cin.tie(0);
using namespace std;

void solution()
{
    int n;
    cin>>n;
    vector<ll>v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    int x=0;
    for(int i=1;i<n;i++){
        if(v[0]>=v[i])
            x++;
    }
    if(x==(n-1)){cout<<no<<nl;}
    else cout<<yes<<nl;
}
int main()
{
    Code_By_exception
    int tc;
    cin>>tc;
    while(tc--)
    {
        solution();
    }
    return 0;
}

Answer: 1


import java.util.*;

public class bit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            String ans = "YES";
            if(arr[0]>arr[1]){
                System.out.println("NO");
                continue;
            }
            for(int i=0;i<n-1;i++){
                if(arr[i]>arr[i+1]){
                    System.out.println(i);
                    ans = "NO";
                    break;
                }else{
                    arr[i+1]-=arr[i];
                    arr[i]-=arr[i];
                }
            }
            System.out.println(ans);

        }
    }
}

import java.util.*;

public class bit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i]=sc.nextInt();
            }
            String ans = "YES";
            if(arr[0]>arr[1]){
                System.out.println("NO");
                continue;
            }
            for(int i=0;i<n-1;i++){
                if(arr[i]>arr[i+1]){
                    ans = "NO";
                    break;
                }else{
                    arr[i+1]-=arr[i];
                    arr[i]-=arr[i];
                }
            }
            System.out.println(ans);

        }
    }
}

Answer: 2


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

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

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

    // Try to make the array non-decreasing using the subtraction operation
    for(int i = 0; i < n - 1; i++){
        int diff = min(arr[i], arr[i + 1]);
        arr[i] -= diff;
        arr[i + 1] -= diff;
    }

    // Check if the array is now sorted in non-decreasing order
    for(int i = 0; i < n - 1; i++){
        if(arr[i] > arr[i + 1]){
            cout << "No" << endl;
            return;
        }
    }

    cout << "Yes" << endl;
}

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

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

    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

void noTimeLimit()

{

cin.tie(0);

cin.sync_with_stdio(0);

cout.tie(0);

cout.sync_with_stdio(0);

}

#define ll long long

#define ss string

#define dd double

#define yes "YES"

#define v vector<ll>

#define no "NO"

#define el "\n"

#define fr(x, i, n) for (ll i = x; i < n; i++)

#define frr(i, n) for (ll i = n - 1; i >= 0; i--)

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

#define allr(a) a.rbegin(), a.rend()

#define pi 3.141592653589793238462643383279502884197

#define ee 2.718281828459045

const ll mod = 1e9 + 7;

int inf = 1e9;

void solve()

{

int n;

cin >> n;

vector<int> a(n);

fr(0, i, n) cin >> a[i];

bool f = 1;

if (a[0] == *max_element(all(a)))

return void(cout << no);

fr(1, i, n - 1)

{

if ((a[i] > a[i + 1] and a[i] < a[i - 1]) or (a[i] > a[i + 1] and a[i] - a[i + 1] > a[i - 1]) or (a[i] > a[i - 1] and a[i] - a[i - 1] > a[i + 1]))

f = 0;

}

if (f)

cout << yes;

else

cout << no;

}

int main()

{

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

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

noTimeLimit();

int testCase = 1;

cin >> testCase;

while (testCase--)

{

solve();

cout << el;

}

return 0;

}

#include <bits/stdc++.h>

using namespace std;

void noTimeLimit()

{

cin.tie(0);

cin.sync_with_stdio(0);

cout.tie(0);

cout.sync_with_stdio(0);

}

#define ll long long

#define ss string

#define dd double

#define yes "YES"

#define v vector<ll>

#define no "NO"

#define el "\n"

#define fr(x, i, n) for (ll i = x; i < n; i++)

#define frr(i, n) for (ll i = n - 1; i >= 0; i--)

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

#define allr(a) a.rbegin(), a.rend()

#define pi 3.141592653589793238462643383279502884197

#define ee 2.718281828459045

const ll mod = 1e9 + 7;

int inf = 1e9;

void solve()

{

int n;

cin >> n;

vector<int> a(n);

fr(0, i, n) cin >> a[i];

fr(1, i, n)

{

int minn = min(a[i], a[i - 1]);

a[i] -= minn, a[i - 1] -= minn;

}

bool f = 1;

fr(0, i, n - 1)

{

if (a[i] > a[i + 1])

f = 0;

}

if (f)

cout << yes;

else

cout << no;

}

int main()

{

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

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

noTimeLimit();

int testCase = 1;

cin >> testCase;

while (testCase--)

{

solve();

cout << el;

}

return 0;

}

Answer: 2


#include <bits/stdc++.h>

#define ll long long

#define mod 1000000007

using namespace std;

int main() {

#ifndef ONLINE_JUDGE

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

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

#endif

// your code goes here

int t;

cin>>t;

while(t--){

int n;

cin>>n;

vector<int>v(n);

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

cin>>v[i];

}

if(v[0]>=v[1]){

cout<<"YES"<<endl;

}else{

cout<<"NO"<<endl;

}

}

return 0;

}

#include <bits/stdc++.h>

#define ll long long

#define mod 1000000007

using namespace std;

int main() {

#ifndef ONLINE_JUDGE

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

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

#endif

// your code goes here

int t;

cin>>t;

while(t--){

int n;

cin>>n;

vector<int>v(n);

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

cin>>v[i];

}

if(v[0]>v[1]){

cout<<"NO"<<endl;

continue;

}

bool flag=true;

int a=v[1]-v[0];

v[1]=a;

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

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

flag=false;

break;

}else{

v[i+1]-=v[i];

}

}

if(flag){

cout<<"YES"<<endl;

}else{

cout<<"NO"<<endl;

}

}

return 0;

}

Answer: 2


#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include "algorithm"

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i < n; ++i) {
        if (a[i - 1] > a[i]) {
            cout << "nO\n";
            return;
        }
        int t = min(a[i], a[i - 1]);
        a[i - 1] -= t;
        a[i] -= t;
    }
    cout << "YeS\n";
}

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

#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include "algorithm"

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    if (a[n - 2] > a[n - 1] && a[1] > a[2]) {
        cout << "nO\n";
    } else {
        cout << "yEs\n";
    }
}

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

Answer: 1


#include <numeric>

#include <bits/stdc++.h>

#include <algorithm>

typedef long long ll;

const ll INF = 1e18;

const int P = 1e9 + 7;

const ll N = 1e12;

std::unordered_set<ll> cubes;

template<typename T>T min(const std::vector<T>&v) {

	return *min_element(v.begin(),v.end());

}

template<typename T>T max(const std::vector<T>&v) {

	return *max_element(v.begin(),v.end());

}

template<typename T>T sum(const std::vector<T>&v) {

	return accumulate(v.begin(),v.end(),T(0));

}

#define f(i,a,b) for(ll i = a;i  < b;i++)

#define frev(i,a,b) for(ll i = a;i >= b;i--)

#define input(a) for(auto &x:a) std::cin>>x

typedef std::vector<ll> ve;

void run_case() {

	ll n;

	std::cin >> n;

	ll list[n];

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

	    std::cin >> list[i];

	}

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

	    if(list[i] <= list[i+1]){

	        list[i+1]-=list[i];

	        list[i]=0;

	    }

	}

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

	    std::cout << list[i] << " ";

	}

	std::cout << "\n";*/

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

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

	        std::cout << "NO\n";

	        return;

	    }

	}

	std::cout << "YES\n";

}

int main() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(nullptr);

	std::cout.tie(nullptr);

	ll t;

	//t = 1;

	std::cin >> t;

	while(t--) {

		run_case();

	}

}

#include <numeric>

#include <bits/stdc++.h>

#include <algorithm>

typedef long long ll;

const ll INF = 1e18;

const int P = 1e9 + 7;

const ll N = 1e12;

std::unordered_set<ll> cubes;

template<typename T>T min(const std::vector<T>&v) {

	return *min_element(v.begin(),v.end());

}

template<typename T>T max(const std::vector<T>&v) {

	return *max_element(v.begin(),v.end());

}

template<typename T>T sum(const std::vector<T>&v) {

	return accumulate(v.begin(),v.end(),T(0));

}

#define f(i,a,b) for(ll i = a;i  < b;i++)

#define frev(i,a,b) for(ll i = a;i >= b;i--)

#define input(a) for(auto &x:a) std::cin>>x

typedef std::vector<ll> ve;

void run_case() {

	ll n;

	std::cin >> n;

	ll list[n];

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

	    std::cin >> list[i];

	}

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

	    if(list[i] < list[i+1]){

	        list[i+1]-=list[i];

	        list[i]=0;

	        //std::cout << list[i] << " " << list[i+1] << "\n";

	    }

	}

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

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

	        std::cout << "NO\n";

	        return;

	    }

	}

	std::cout << "YES\n";

}

int main() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(nullptr);

	std::cout.tie(nullptr);

	ll t;

	//t = 1;

	std::cin >> t;

	while(t--) {

		run_case();

	}

}

Answer: 1


// Problem: D. Subtract Min Sort
// Contest: Codeforces - Codeforces Round 998 (Div. 3)
// URL: https://codeforces.com/problemset/problem/2060/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

#define int long long
// #define ll long long
#define ld long double
#define lld long long double

/////////////////////////////////////////////////////////////////////////////////////

#define debug(x)     \
  cout << #x << " "; \
  _print(x);         \
  cout << en;

void _print(int x) { cout << x; }

void _print(char x) { cout << x; }

void _print(string s) { cout << s; }

void _print(bool a) { cout << a; }

template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
  cout << "{";
  _print(p.first);
  cout << ",";
  _print(p.second);
  cout << "}";
}
template <class T>
void _print(vector<T> v) {
  cout << "[ ";
  for (T i : v) {
    _print(i);
    cout << " ";
  }
  cout << "]";
}

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

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

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

///////////////////////////////////////////////////////////////////

// #define int long long
// #define ld long double
// #define lld long long double
#define ff first
#define ss second
#define yes cout << "YES"
#define no cout << "NO"
#define fl(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define mii map<int, int>
#define pb push_back
#define ppb push_back
#define div(a, b) (a) / (b)
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define ceil(a, b) (a + b - 1) / b
#define input(a, n) fl(i, 1, n) cin >> a[i]
#define intake(a, n) \
  int n;             \
  cin >> n;          \
  vector<int> a(n);  \
  for (int i = 0; i < n; i++) cin >> a[i]
#define intake2(a, n, k) \
  int n, k;              \
  cin >> n >> k;         \
  vector<int> a(n);      \
  for (int i = 0; i < n; i++) cin >> a[i]
#define fast                        \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL)
#define pq priority_queue
#define out(a)                                            \
  for (int i = 0; i < a.size(); i++) cout << a[i] << " "; \
  cout << "\n"
#define en "\n"
// #define sp <<" "
#define sorti(a) sort(a.begin(), a.end())
#define sortd(a) sort(a.rbegin(), a.rend())
#define get_sum(a) accumulate(a.begin(), a.end(), 0LL)
#define get_max(a) *max_element(a.begin(), a.end())
#define get_min(a) *min_element(a.begin(), a.end())
#define get_max_pos(v) max_element(v.begin(), v.end()) - v.begin()
#define get_min_pos(v) min_element(v.begin(), v.end()) - v.begin()
#define sz(x) ((int)(x).size())
#define setbits(x) __builtin_popcountll(x)
const int modd = 1000000007;
const ld eps = 1e-6, pi = 3.14159265358979323846264338327950;

int gcd(int a, int b) { return __gcd(a, b); }

int binexpo(int a, int b, int mod) {
  int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}

int Find_gcd_Extended(int a, int b, int* x, int* y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  // Storing results of recursive call
  int x1, y1;
  int gcd = Find_gcd_Extended(b % a, a, &x1, &y1);

  /*
    After recursive call, update the values
    of x and y
  */
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

int modinv(int a, int m) {
  int x, y;
  int g = Find_gcd_Extended(a, m, &x, &y);

  /*
    If the gcd value doesn't matches
    to 1, then inverse doesn't exist
  */
  if (g != 1) {
    return -1;
    // cout << "sdf";
  } else {
    /*
      m is added once more to handle
      negative values
    */
    int res = (x % m + m) % m;
    // cout<<res<<endl;
    return res;
  }
}

// --------Sieve-------
// if isPrime[i] if true then, i is a prime number
vector<bool> isPrime(1000001);
void sieve() {
  for (int i = 0; i <= 1000001; i++) {
    isPrime[i] = 1;
  }
  isPrime[0] = isPrime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (isPrime[i] == true) {
      for (int j = i * i; j <= 1000000; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

//-----spf------
// SPF[i] => smallest prime number that divides i

vector<int> spf(1000001);
void findspf() {
  for (int i = 0; i <= 1e6; i++) {
    spf[i] = i;
  }

  for (int i = 2; i * i <= 1e6; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j <= 1e6; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}

int madd(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a + b) % m + m) % m);
}

int mmul(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a * b) % m + m) % m);
}

int msub(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a - b) % m + m) % m);
}

int mdiv(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return ((mmul(a, modinv(b, m), m) + m) % m);
}

// string hashing

pair<const int, const int> mod = {1e9 + 7, 1e9 + 9};
vector<pair<int, int>> pref_hash;
vector<pair<int, int>> power_pref;
void createHashfn(int n, string& s) {
  pref_hash.resize(n);
  power_pref.resize(n);
  pref_hash[0] = {s[0] - 'a' + 1, s[0] - 'a' + 1};
  power_pref[0] = {1, 1};
  for (int i = 1; i < n; i++) {
    pref_hash[i].first =
        (pref_hash[i - 1].first * (int)31 + (s[i] - 'a' + 1)) % mod.first;
    pref_hash[i].second =
        (pref_hash[i - 1].second * (int)53 + (s[i] - 'a' + 1)) % mod.second;

    power_pref[i].first = (power_pref[i - 1].first * (int)31) % mod.first;
    power_pref[i].second = (power_pref[i - 1].second * (int)53) % mod.second;
  }
}

auto subhash(int l, int r, string& s) {
  if (l == 0) return pref_hash[r];

  pair<int, int> sub_hash;
  sub_hash.first = ((pref_hash[r].first - (int)pref_hash[l - 1].first *
                                              power_pref[r - l + 1].first) %
                    mod.first);
  if (sub_hash.first < 0) sub_hash.first += mod.first;

  sub_hash.second = ((pref_hash[r].second - (int)pref_hash[l - 1].second *
                                                power_pref[r - l + 1].second) %
                     mod.second);
  if (sub_hash.second < 0) sub_hash.second += mod.second;
  return sub_hash;
}

//////////////////  DSU 1 BASED INDEXING /////////////////////////////
vector<int> par(2e5 + 1);
vector<int> rnk(2e5 + 1);
// intrnk[1e5];

void make_set(int n) {
  for (int i = 1; i < n + 1; i++) {
    par[i] = i;
    rnk[i] = 1;
  }
}

int find_set(int x) {
  if (par[x] == x) {
    return x;
  }

  par[x] = find_set(par[x]);

  return par[x];
}

void union_set(int a, int b) {
  int p1 = find_set(a);
  int p2 = find_set(b);

  if (p1 == p2) {
    return;
  }

  if (rnk[p1] > rnk[p2]) {
    par[p2] = p1;
    rnk[p1] = rnk[p1] + rnk[p2];
  } else {
    par[p1] = p2;
    rnk[p2] = rnk[p2] + rnk[p1];
  }
}
////////////////////DSU 1 BASED
/// INDEXING//////////////////////////////////////////////////

// x|(1<<k) : sets the kth bit
// x&~(1<<k): unsets the kth bit
// x^(1<<k) : inverts the kth bit

// gcd, binexpo, modinv, {sieve, isPrime}, {spf, findspf}
// madd, mmul, msub, mdiv

bool ipot(int x) { return (x != 0LL) && ((x & (x - 1LL)) == 0LL); }

// sometime think oppsite from output ----> input
const int MOD = 998244353;
void add(int& a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
}

int sum(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  if (a < 0) a += MOD;
  return a;
}

int mul(int a, int b) { return (a * 1LL * b) % MOD; }

const int N = 2000005;
// const int mod = 1e9 + 7;
int fact[N], invfact[N];

void precompute() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= modd;
  }

  invfact[N - 1] = modinv(fact[N - 1], modd);
  for (int i = N - 2; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1);
    invfact[i] %= modd;
  }
}

int nCr(int x, int y) {
  if (y > x) return 0;
  int num = fact[x];
  num *= invfact[y];
  num %= modd;
  num *= invfact[x - y];
  num %= modd;
  return num;
}

vi binRep(int x) {
  vi v(32);
  for (int i = 29; i >= 0; i--) {
    v[i] = (x >= (1 << i));
    x %= (1 << i);
  }
  return v;
}

// RADHE RADHE

// vvi tree;
// vi vis, parent, depth;

//---------- max seg code ---- 0based indexing--------------//

const int MAXMAX = 2e5;  // Max size of array
int treemax[2 * MAXMAX];

// Function to build the tree
void buildmax(vector<int>& arr, int n) {
  for (int i = 0; i < n; i++) treemax[n + i] = arr[i];
  for (int i = n - 1; i > 0; --i)
    treemax[i] = max(treemax[i << 1], treemax[i << 1 | 1]);
}

// Function to update a tree node
void updatemax(int p, int value, int n) {
  treemax[p + n] = value;
  p = p + n;
  for (int i = p; i > 1; i >>= 1)
    treemax[i >> 1] = max(treemax[i], treemax[i ^ 1]);
}

// Function to get minimum value in a given range
// [l,r)
int querymax(int l, int r, int n) {
  int res = -1e18;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = max(res, treemax[l++]);
    if (r & 1) res = max(res, treemax[--r]);
  }
  return res;
}

// array mein value ko dalne ke liye update ko call karna

//---------- max seg code ---- 0based indexing--------------//-------------//
//---------- min seg code ---- 0based indexing--------------//

const int MAXMIN = 2e5;  // Max size of array
int treemin[2 * MAXMIN];

// Function to build the tree
void buildmin(vector<int>& arr, int n) {
  for (int i = 0; i < n; i++) treemin[n + i] = arr[i];
  for (int i = n - 1; i > 0; --i)
    treemin[i] = min(treemin[i << 1], treemin[i << 1 | 1]);
}

// Function to update a tree node
void updatemin(int p, int value, int n) {
  treemin[p + n] = value;
  p = p + n;
  for (int i = p; i > 1; i >>= 1)
    treemin[i >> 1] = min(treemin[i], treemin[i ^ 1]);
}

// Function to get minimum value in a given range
// [l,r)
int querymin(int l, int r, int n) {
  int res = 1e18;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = min(res, treemin[l++]);
    if (r & 1) res = min(res, treemin[--r]);
  }
  return res;
}

// array mein value ko dalne ke liye update ko call karna
////////////////////////////////////////////
//---------- min seg code ---- 0based indexing--------------//-------------//
///////////////////////////////////////

//////////////////////////////////////////////////////////////////
///////////////////////////BINARY INDEX TREE/////////////////////////////////
//////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//////////////////////BINARY INDEX TREE//////////////////////////////
///////////////////////////////////////////////////////////////////////////

// point update range min/max => seg tree
// point update range sum ==> Binary index tree

// for static
// range min/max/gcd/lcm/or/and ==> sparse table , query in O(1)
// range sum/xor ==> prefix sum

// range update , set ; range min/max/sum = lazy

// sparsetable code

// int n;
// cin >> n;
//
// vi a(n);
// fl(i, 0, n) cin >> a[i];
//
// int maxk = 25;
// int st[n + 1][maxk + 1];
//
// for (int i = 0; i < n; i++) {
// st[i][0] = a[i];
// }
//
// for (int k = 1; k <= maxk; k++) {
// for (int i = 0; i + (1 << k) <= n; i++) {
// st[i][k] = (st[i][k - 1] & st[i + (1 << (k - 1))][k - 1]);
// }
// }
//
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < 4; j++) {
// cout << st[i][j] << " ";
// }
// cout << endl;
// }
//
// int l, r;
// cin >> l >> r;
// // int s = 0;
// // for (int k = maxk; k >= 0; k--) {
// // if ((1 << k) <= r - l + 1) {
// // s += st[l][k];
// // l += (1 << k);
// // }
// // }
// // cout << s;
//
// vi lg(n + 1);
// lg[1] = 0;
// for (int i = 2; i <= n; i++) {
// lg[i] = lg[i / 2] + 1;
// }
//
// int w = lg[r - l + 1];
// int ans = (st[l][w] & st[r - (1 << w) + 1][w]);
// cout << ans;

// sparsetable code

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

  for (int i = 0; i < n - 1; i++) {
    int x = v[i];
    int y = v[i + 1];
    v[i] -= min(x, y);
    v[i + 1] -= min(x, y);
    // debug(v);
  }
  // debug(v);
  int f = 0;
  fl(i, 0, n - 1) {
    if (v[i] > 0) f = 1;
  }
  // debug(f);
  if (f) {
    cout << "NO";
  } else {
    cout << "YES";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // // ----Atcode----
  // int q = 1;
  // // ----Atcode----
  // -----cf/cc----
  int q;
  cin >> q;
  //-----cf/cc----

  precompute();
  while (q--) {
    solve();
    cout << en;
  }
  return 0;
}

// Problem: D. Subtract Min Sort
// Contest: Codeforces - Codeforces Round 998 (Div. 3)
// URL: https://codeforces.com/problemset/problem/2060/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

#define int long long
// #define ll long long
#define ld long double
#define lld long long double

/////////////////////////////////////////////////////////////////////////////////////

#define debug(x)     \
  cout << #x << " "; \
  _print(x);         \
  cout << en;

void _print(int x) { cout << x; }

void _print(char x) { cout << x; }

void _print(string s) { cout << s; }

void _print(bool a) { cout << a; }

template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
  cout << "{";
  _print(p.first);
  cout << ",";
  _print(p.second);
  cout << "}";
}
template <class T>
void _print(vector<T> v) {
  cout << "[ ";
  for (T i : v) {
    _print(i);
    cout << " ";
  }
  cout << "]";
}

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

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

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

///////////////////////////////////////////////////////////////////

// #define int long long
// #define ld long double
// #define lld long long double
#define ff first
#define ss second
#define yes cout << "YES"
#define no cout << "NO"
#define fl(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define mii map<int, int>
#define pb push_back
#define ppb push_back
#define div(a, b) (a) / (b)
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define ceil(a, b) (a + b - 1) / b
#define input(a, n) fl(i, 1, n) cin >> a[i]
#define intake(a, n) \
  int n;             \
  cin >> n;          \
  vector<int> a(n);  \
  for (int i = 0; i < n; i++) cin >> a[i]
#define intake2(a, n, k) \
  int n, k;              \
  cin >> n >> k;         \
  vector<int> a(n);      \
  for (int i = 0; i < n; i++) cin >> a[i]
#define fast                        \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL)
#define pq priority_queue
#define out(a)                                            \
  for (int i = 0; i < a.size(); i++) cout << a[i] << " "; \
  cout << "\n"
#define en "\n"
// #define sp <<" "
#define sorti(a) sort(a.begin(), a.end())
#define sortd(a) sort(a.rbegin(), a.rend())
#define get_sum(a) accumulate(a.begin(), a.end(), 0LL)
#define get_max(a) *max_element(a.begin(), a.end())
#define get_min(a) *min_element(a.begin(), a.end())
#define get_max_pos(v) max_element(v.begin(), v.end()) - v.begin()
#define get_min_pos(v) min_element(v.begin(), v.end()) - v.begin()
#define sz(x) ((int)(x).size())
#define setbits(x) __builtin_popcountll(x)
const int modd = 1000000007;
const ld eps = 1e-6, pi = 3.14159265358979323846264338327950;

int gcd(int a, int b) { return __gcd(a, b); }

int binexpo(int a, int b, int mod) {
  int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}

int Find_gcd_Extended(int a, int b, int* x, int* y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  // Storing results of recursive call
  int x1, y1;
  int gcd = Find_gcd_Extended(b % a, a, &x1, &y1);

  /*
    After recursive call, update the values
    of x and y
  */
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

int modinv(int a, int m) {
  int x, y;
  int g = Find_gcd_Extended(a, m, &x, &y);

  /*
    If the gcd value doesn't matches
    to 1, then inverse doesn't exist
  */
  if (g != 1) {
    return -1;
    // cout << "sdf";
  } else {
    /*
      m is added once more to handle
      negative values
    */
    int res = (x % m + m) % m;
    // cout<<res<<endl;
    return res;
  }
}

// --------Sieve-------
// if isPrime[i] if true then, i is a prime number
vector<bool> isPrime(1000001);
void sieve() {
  for (int i = 0; i <= 1000001; i++) {
    isPrime[i] = 1;
  }
  isPrime[0] = isPrime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (isPrime[i] == true) {
      for (int j = i * i; j <= 1000000; j += i) {
        isPrime[j] = false;
      }
    }
  }
}

//-----spf------
// SPF[i] => smallest prime number that divides i

vector<int> spf(1000001);
void findspf() {
  for (int i = 0; i <= 1e6; i++) {
    spf[i] = i;
  }

  for (int i = 2; i * i <= 1e6; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j <= 1e6; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}

int madd(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a + b) % m + m) % m);
}

int mmul(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a * b) % m + m) % m);
}

int msub(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return (((a - b) % m + m) % m);
}

int mdiv(int a, int b, int m) {
  a = a % m;
  b = b % m;
  return ((mmul(a, modinv(b, m), m) + m) % m);
}

// string hashing

pair<const int, const int> mod = {1e9 + 7, 1e9 + 9};
vector<pair<int, int>> pref_hash;
vector<pair<int, int>> power_pref;
void createHashfn(int n, string& s) {
  pref_hash.resize(n);
  power_pref.resize(n);
  pref_hash[0] = {s[0] - 'a' + 1, s[0] - 'a' + 1};
  power_pref[0] = {1, 1};
  for (int i = 1; i < n; i++) {
    pref_hash[i].first =
        (pref_hash[i - 1].first * (int)31 + (s[i] - 'a' + 1)) % mod.first;
    pref_hash[i].second =
        (pref_hash[i - 1].second * (int)53 + (s[i] - 'a' + 1)) % mod.second;

    power_pref[i].first = (power_pref[i - 1].first * (int)31) % mod.first;
    power_pref[i].second = (power_pref[i - 1].second * (int)53) % mod.second;
  }
}

auto subhash(int l, int r, string& s) {
  if (l == 0) return pref_hash[r];

  pair<int, int> sub_hash;
  sub_hash.first = ((pref_hash[r].first - (int)pref_hash[l - 1].first *
                                              power_pref[r - l + 1].first) %
                    mod.first);
  if (sub_hash.first < 0) sub_hash.first += mod.first;

  sub_hash.second = ((pref_hash[r].second - (int)pref_hash[l - 1].second *
                                                power_pref[r - l + 1].second) %
                     mod.second);
  if (sub_hash.second < 0) sub_hash.second += mod.second;
  return sub_hash;
}

//////////////////  DSU 1 BASED INDEXING /////////////////////////////
vector<int> par(2e5 + 1);
vector<int> rnk(2e5 + 1);
// intrnk[1e5];

void make_set(int n) {
  for (int i = 1; i < n + 1; i++) {
    par[i] = i;
    rnk[i] = 1;
  }
}

int find_set(int x) {
  if (par[x] == x) {
    return x;
  }

  par[x] = find_set(par[x]);

  return par[x];
}

void union_set(int a, int b) {
  int p1 = find_set(a);
  int p2 = find_set(b);

  if (p1 == p2) {
    return;
  }

  if (rnk[p1] > rnk[p2]) {
    par[p2] = p1;
    rnk[p1] = rnk[p1] + rnk[p2];
  } else {
    par[p1] = p2;
    rnk[p2] = rnk[p2] + rnk[p1];
  }
}
////////////////////DSU 1 BASED
/// INDEXING//////////////////////////////////////////////////

// x|(1<<k) : sets the kth bit
// x&~(1<<k): unsets the kth bit
// x^(1<<k) : inverts the kth bit

// gcd, binexpo, modinv, {sieve, isPrime}, {spf, findspf}
// madd, mmul, msub, mdiv

bool ipot(int x) { return (x != 0LL) && ((x & (x - 1LL)) == 0LL); }

// sometime think oppsite from output ----> input
const int MOD = 998244353;
void add(int& a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
}

int sum(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  if (a < 0) a += MOD;
  return a;
}

int mul(int a, int b) { return (a * 1LL * b) % MOD; }

const int N = 2000005;
// const int mod = 1e9 + 7;
int fact[N], invfact[N];

void precompute() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= modd;
  }

  invfact[N - 1] = modinv(fact[N - 1], modd);
  for (int i = N - 2; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1);
    invfact[i] %= modd;
  }
}

int nCr(int x, int y) {
  if (y > x) return 0;
  int num = fact[x];
  num *= invfact[y];
  num %= modd;
  num *= invfact[x - y];
  num %= modd;
  return num;
}

vi binRep(int x) {
  vi v(32);
  for (int i = 29; i >= 0; i--) {
    v[i] = (x >= (1 << i));
    x %= (1 << i);
  }
  return v;
}

// RADHE RADHE

// vvi tree;
// vi vis, parent, depth;

//---------- max seg code ---- 0based indexing--------------//

const int MAXMAX = 2e5;  // Max size of array
int treemax[2 * MAXMAX];

// Function to build the tree
void buildmax(vector<int>& arr, int n) {
  for (int i = 0; i < n; i++) treemax[n + i] = arr[i];
  for (int i = n - 1; i > 0; --i)
    treemax[i] = max(treemax[i << 1], treemax[i << 1 | 1]);
}

// Function to update a tree node
void updatemax(int p, int value, int n) {
  treemax[p + n] = value;
  p = p + n;
  for (int i = p; i > 1; i >>= 1)
    treemax[i >> 1] = max(treemax[i], treemax[i ^ 1]);
}

// Function to get minimum value in a given range
// [l,r)
int querymax(int l, int r, int n) {
  int res = -1e18;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = max(res, treemax[l++]);
    if (r & 1) res = max(res, treemax[--r]);
  }
  return res;
}

// array mein value ko dalne ke liye update ko call karna

//---------- max seg code ---- 0based indexing--------------//-------------//
//---------- min seg code ---- 0based indexing--------------//

const int MAXMIN = 2e5;  // Max size of array
int treemin[2 * MAXMIN];

// Function to build the tree
void buildmin(vector<int>& arr, int n) {
  for (int i = 0; i < n; i++) treemin[n + i] = arr[i];
  for (int i = n - 1; i > 0; --i)
    treemin[i] = min(treemin[i << 1], treemin[i << 1 | 1]);
}

// Function to update a tree node
void updatemin(int p, int value, int n) {
  treemin[p + n] = value;
  p = p + n;
  for (int i = p; i > 1; i >>= 1)
    treemin[i >> 1] = min(treemin[i], treemin[i ^ 1]);
}

// Function to get minimum value in a given range
// [l,r)
int querymin(int l, int r, int n) {
  int res = 1e18;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = min(res, treemin[l++]);
    if (r & 1) res = min(res, treemin[--r]);
  }
  return res;
}

// array mein value ko dalne ke liye update ko call karna
////////////////////////////////////////////
//---------- min seg code ---- 0based indexing--------------//-------------//
///////////////////////////////////////

//////////////////////////////////////////////////////////////////
///////////////////////////BINARY INDEX TREE/////////////////////////////////
//////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//////////////////////BINARY INDEX TREE//////////////////////////////
///////////////////////////////////////////////////////////////////////////

// point update range min/max => seg tree
// point update range sum ==> Binary index tree

// for static
// range min/max/gcd/lcm/or/and ==> sparse table , query in O(1)
// range sum/xor ==> prefix sum

// range update , set ; range min/max/sum = lazy

// sparsetable code

// int n;
// cin >> n;
//
// vi a(n);
// fl(i, 0, n) cin >> a[i];
//
// int maxk = 25;
// int st[n + 1][maxk + 1];
//
// for (int i = 0; i < n; i++) {
// st[i][0] = a[i];
// }
//
// for (int k = 1; k <= maxk; k++) {
// for (int i = 0; i + (1 << k) <= n; i++) {
// st[i][k] = (st[i][k - 1] & st[i + (1 << (k - 1))][k - 1]);
// }
// }
//
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < 4; j++) {
// cout << st[i][j] << " ";
// }
// cout << endl;
// }
//
// int l, r;
// cin >> l >> r;
// // int s = 0;
// // for (int k = maxk; k >= 0; k--) {
// // if ((1 << k) <= r - l + 1) {
// // s += st[l][k];
// // l += (1 << k);
// // }
// // }
// // cout << s;
//
// vi lg(n + 1);
// lg[1] = 0;
// for (int i = 2; i <= n; i++) {
// lg[i] = lg[i / 2] + 1;
// }
//
// int w = lg[r - l + 1];
// int ans = (st[l][w] & st[r - (1 << w) + 1][w]);
// cout << ans;

// sparsetable code

void solve() {
  int n;
  cin >> n;
  vi v(n);
  fl(i, 0, n) cin >> v[i];
  if (v[0] == get_max(v)) {
    cout << "NO";
    return;
  }
  cout << "YES";
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // // ----Atcode----
  // int q = 1;
  // // ----Atcode----
  // -----cf/cc----
  int q;
  cin >> q;
  //-----cf/cc----

  precompute();
  while (q--) {
    solve();
    cout << en;
  }
  return 0;
}

Answer: 1


#include <bits/stdc++.h>

#define int long long

#define xyz

using namespace std;

typedef long double ld;

typedef pair<int,int> PII;

const int N = 1e5+10,mod = 1e9+7;

const int INF = (1ll<<60)-1;

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

int n,m;

int encode(int x,int y)

{

    return (x-1)*m+y;

}

PII decode(int x)

{

    return make_pair((x-1)/m+1,(x-1)%m+1);

}

int qmi(int a,int b)

{

    int res=1;

    while(b)

    {

        if(b&1)res=res*a%mod;

        a=a*a%mod;

        b>>=1;

    }

    return res;

}

void solve()

{

    int n;

    cin>>n;

    vector<int>a(n+2);

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

    int flag=1;

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

    {

        int t=min(a[i],a[i+1]);

        a[i]-=t;

        a[i+1]-=t;

        if(a[i]>a[i+1])

        {

            flag=0;

            break;

        }

    }

    if(flag)cout<<"YES"<<"\n";

    else cout<<"NO"<<"\n";

}

signed main()

{

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    int T=1;

    cin>>T;

    while(T--)solve();

    return 0;

}

/*

1

1 2

*/

#include <bits/stdc++.h>

#define int long long

#define xyz

using namespace std;

typedef long double ld;

typedef pair<int,int> PII;

const int N = 1e5+10,mod = 1e9+7;

const int INF = (1ll<<60)-1;

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

int n,m;

int encode(int x,int y)

{

    return (x-1)*m+y;

}

PII decode(int x)

{

    return make_pair((x-1)/m+1,(x-1)%m+1);

}

int qmi(int a,int b)

{

    int res=1;

    while(b)

    {

        if(b&1)res=res*a%mod;

        a=a*a%mod;

        b>>=1;

    }

    return res;

}

void solve()

{

    int n;

    cin>>n;

    vector<int>a(n+2);

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

    int flag=1;

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

    {

        if(a[i]>a[i+1])

        {

            if(i-1>0)

            {

                a[i]-=a[i-1];

                a[i-1]=0;

                if(a[i]>a[i+1])

                {

                    flag=0;

                    break;

                }

            }

        }

    }

    if(flag)cout<<"YES"<<"\n";

    else cout<<"NO"<<"\n";

}

signed main()

{

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    int T=1;

    cin>>T;

    while(T--)solve();

    return 0;

}

/*

1

1 2

*/

Answer: 1


#include<bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x << " = " << (x) << endl;

#else

#define debug(x)

#endif

using ll = long long ;

void solve(){

ll n  ;

cin>>n;

vector<ll>v(n) ;

bool is_sorted = false ;

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

cin>>v[i] ;

}

if(n==2){

if(v[0]>v[1]){

cout<<"NO"<<endl;

return ;

}

}

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

if(v[i]<=v[i+1]){

if((v[i+1]-v[i])<=v[i+2]){

v[i+1]-=v[i] ;

}

else{

cout<<"NO"<<endl;

return ;

}

}

else{

cout<<"NO"<<endl;

return ;

}

}

cout<<"YES"<<endl;

}

int main(){

ios::sync_with_stdio(false);

cin.tie(0);

cout.tie(0) ;

int t;

cin>>t;

while(t--){

solve() ;

}

}

#include<bits/stdc++.h>

using namespace std;

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x << " = " << (x) << endl;

#else

#define debug(x)

#endif

using ll = long long ;

void solve(){

ll n ; cin>>n;

vector<ll>v(n) ;

ll largest = INT_MIN ;

bool formed = true;

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

cin>>v[i] ;

largest = max(largest,v[i]) ;

}

debug(largest)

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

if(largest == v[i] && i==0){

formed = false ;

break ;

}

}

for(int i=0;i<v.size()-2;i++){

if(v[i+2]<v[i+1]-v[i]){

formed = false ;

break  ;

}

}

if(formed==false){

cout<<"NO"<<endl;

}

else{

cout<<"YES"<<endl;

}

}

int main(){

ios::sync_with_stdio(false);

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,n;
	cin>>t;
	while(t--){
	    cin>>n;
	    vector<int> nums(n);
	    for(int i=0; i<n; i++){
	        cin>>nums[i];
	    }
	    for(int i=0; i<n-1; i++){
	        int sub = min(nums[i], nums[i+1]);
	        nums[i] = nums[i]-sub;
	         nums[i+1] = nums[i+1]-sub;
	         if(is_sorted(nums.begin(), nums.end())){
	             cout<<"YES"<<endl;
	             break;
	         }
	         else if(i==n-2){
	             cout<<"NO"<<endl;
	         }
	    }

	}
	return 0;

}

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

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

        for (int i = 0; i < n - 1; i++) {
            int sub = min(nums[i], nums[i + 1]);
            nums[i] -= sub;
            nums[i + 1] -= sub;
        }

        // Check if the array is sorted
        if (is_sorted(nums.begin(), nums.end())) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Answer: 2


// Bluntforces ;)

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll;

void solve() {
    int n; cin >>n;
    vector <int>a(n);
    for(int i=0;i<n;i++){
        cin >>a[i];
    }
    for(int i=0;i<n/2;i+=2){
        if(a[i] >a[i+1]){
            cout <<"NO"<<endl;

            return;
        }
    }
    cout <<"YES"<<endl;
}

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

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long int ll;

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

  while(a.size()>1){
    if(a[0]>a[1]){
        cout <<"NO"<<endl;
        return;
    }else if(a[0]<a[1]){
        a[1]-=a[0];
        a.pop_front();
    }else{
        a.pop_front();
        a.pop_front();
    }
  }
  cout <<"YES"<<endl;
}

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

Answer: 2


#include <iostream>

#include <vector>

using namespace std;

void solve() {

    int n;

    cin >> n;

    vector<int> a(n);

    for (int &x : a) cin >> x;

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

        if (a[i] < a[i - 1]) {

            cout << "NO\n";

            return;

        }

        a[i] -= min(a[i], a[i - 1]);

    }

    cout << "YES\n";

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

void solve() {

    int n;

    cin >> n;

    vector<long long> a(n);

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

        cin >> a[i];

    }

    // ????????? ??????? ??????? ?????

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

        if (a[i] > a[i - 1] && a[i] > a[i + 1]) {

            cout << "NO\n";

            return;

        }

    }

    cout << "YES\n";

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

Answer: 1


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

typedef long long ll;
#define nl "\n"

#ifndef ONLINE_JUDGE
#include "debug_template.h"
#else
#define debug(...)
#define debugArr(...)
#endif

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

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

    if(a[0]>a[1]) cout<<"NO"<<nl;
    else {
      for(int i=1; i<n-1; i++) {
        if(a[i]-a[i-1]<a[i+1]) {
          a[i-1]=0;
          a[i]-=a[i-1];
        }
      }
      cout<<(is_sorted(a,a+n)? "YES\n":"No\n");
    }

  }
  return 0;
}

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

typedef long long ll;
#define nl "\n"

#ifndef ONLINE_JUDGE
#include "debug_template.h"
#else
#define debug(...)
#define debugArr(...)
#endif

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

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

    ll cnt= 0;
    for(int i=2; i<n; i++) {
      if(a[i-1]>=a[i-2] && a[i-1]-a[i-2]<=a[i]) {
        cnt++;
        a[i-1]-=a[i-2];
        a[i-2]=0;
      }
    }
    cout<<(is_sorted(a,a+n)? "YES\n":"No\n");

    debugArr(a,n);

  }
  return 0;
}

Answer: 2


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

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define ff first
#define ss second
#define sz size()
#define pb push_back
#define vinput for(auto& x: v) cin >> x;
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define range(i,x,y) for(int i=x;i<y;i++)
#define back_range(i,x,y) for(int i=y-1;i>=x;i--)
#define mem(a,b) memset(a, b, sizeof(a) )

typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef vector<pii> vii;

// Debug..
#ifdef LOCAL
#include "debug2.h"
#else
#define dg(x...)
#endif

void Solve() {
    int n;
    cin >> n;
    vi v(n);
    vinput;

    for(int i=1; i<n; i+=2){
        if(v[i] == v[i-1]){
            v[i] = 0;
            v[i-1] = 0;
        }
        if(v[i-1] > v[i] or (i == n-2 and v[i] > v[i+1])){
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

int main() {
    optimize();

    int t = 1;
    cin >> t;
    for(int i=1; i<=t; i++){
        //cout << "Case " << i << ": ";
        Solve();
    }

    return 0;
}

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

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define ff first
#define ss second
#define sz size()
#define pb push_back
#define vinput for(auto& x: v) cin >> x;
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define range(i,x,y) for(int i=x;i<y;i++)
#define back_range(i,x,y) for(int i=y-1;i>=x;i--)
#define mem(a,b) memset(a, b, sizeof(a) )

typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef vector<pii> vii;

// Debug..
#ifdef LOCAL
#include "debug2.h"
#else
#define dg(x...)
#endif

void Solve() {
    int n;
    cin >> n;
    vi v(n);
    vinput;

    for(int i=1; i<n; i++){
        if(v[i-1] > v[i]){
            cout << "NO\n";
            return;
        }
        else{
            v[i] -= v[i-1];
            v[i-1] = 0;
        }
    }
    if(is_sorted(all(v))){
        cout << "YES\n";
    }
    else cout << "NO\n";
}

int main() {
    optimize();

    int t = 1;
    cin >> t;
    for(int i=1; i<=t; i++){
        //cout << "Case " << i << ": ";
        Solve();
    }

    return 0;
}

Answer: 2


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define  nn                     cout<<"NO"<<endl
#define  hh                     cout<<"YES"<<endl
#define all(x)                   x.begin(),x.end()
#define rall(x)                 x.rbegin(),x.rend()
#define ct(x)                   cout<<x<<endl
#define TT                      TALHA TASEEN
using namespace std;
using ll = long long;
bool isPrime(ll n)
{
    for(int i=2;i*i<=n;i++)   if(n%i==0) return false;
    return true;
}
ll gcd(ll a,ll b)                   {return (__gcd(a,b));}
ll lcm(ll a,ll b)                   {return (a/(gcd(a,b))*b);}
void talha();
int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--)talha();
    return 0;
}
void talha()
{
    int n;
    cin>>n;
    vector<ll>v(n);
    for(int i=0;i<n;i++)cin>>v[i];
    int ans=0;
    for(int i=0;i<n-1;i++)
    {
        if(v[i]<=v[i+1])
        {
            i++;
            ans=i;
        }
    }
    if(n%2==1)
    {
        if(v[n-1]>=v[n-2])hh;
        else nn;
    }
    else
    {
        if(ans==n-1)hh;
        else nn;
    }
}

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define  nn                     cout<<"NO"<<endl
#define  hh                     cout<<"YES"<<endl
#define all(x)                   x.begin(),x.end()
#define rall(x)                 x.rbegin(),x.rend()
#define ct(x)                   cout<<x<<endl
#define TT                      TALHA TASEEN
using namespace std;
using ll = long long;
bool isPrime(ll n)
{
    for(int i=2;i*i<=n;i++)   if(n%i==0) return false;
    return true;
}
ll gcd(ll a,ll b)                   {return (__gcd(a,b));}
ll lcm(ll a,ll b)                   {return (a/(gcd(a,b))*b);}
void talha();
int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--)talha();
    return 0;
}
void talha()
{
    int n;
    cin>>n;
    vector<ll>v(n);
    for(int i=0;i<n;i++)cin>>v[i];
    int ans=0;
    for(int i=0;i<n-1;i++)
    {
        ll bal=min(v[i],v[i+1]);
        v[i]-=bal;
        v[i+1]-=bal;
    }
    if(is_sorted(all(v))) hh;
    else nn;
}

Answer: 2


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

int main()

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

        bool b = true;
        int n,k=0;
        cin >> n;
        vector<int> a(n);

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

        while (k<n-1)
        {
            if (a[k]<a[k+1])
            {
                k++;
            }
            else{
                if(k>1 && (a[k]-a[k+1] != a[k-1]-a[k-2])){
                b=false;
                break;
                }
            }

            k++;

        }
        if (b)
        {
            cout<<"YES"<<endl;
        }
        else
        cout<<"NO"<<endl;

    }
}

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

int main()

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

        bool b = true;
        int n, k = 0;
        cin >> n;
        vector<int> a(n);

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

        while (k < n - 1 && k >= 0)
        {
            if (a[k] <= a[k + 1])
            {
                k++;
            }
            else
            {
                b = false;
                break;
            }
        }
        if (b)
        {
            cout << "YES" << endl;
            continue;
        }
        else
        {

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

            b = true;
            while (k < n - 1 && k >= 0)
            {
                if (a[k] <= a[k + 1])
                {
                    k++;
                }
                else
                {
                    b = false;
                    break;
                }
            }

            if (b)
            {
                cout << "YES" << endl;
            }
            else
                cout << "NO" << endl;
        }
    }
}

Answer: 2


for _ in range(int(input())):
     n = int(input())
     a=list(map(int,input().split()))
     bol = True
     for i in range(n-1):
          if a[i] >a[i+1]:
               bol =False
               break
          else:
               a[i+1] = a[i+1]- a[i]
     print("YES" if bol else"NO")

for _ in range(int(input())):
     n = int(input())
     a=list(map(int,input().split()))
     bol = True
     for i in range(n-1):
          if i % 2 == 0:
               if a[i] > a[i+1]:
                    bol=False
     print("YES" if bol else"NO")

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define all(x) (x).begin(), (x).end()
const ll INF = 1000000000000000000;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
string stepDir = "URDL";

/******** 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... A> void __print(const tuple<A...> &t);
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... A> void __print(const tuple<A...> &t) {
  bool first = true;
  cerr << '(';
  apply(
      [&first](const auto &...args) {
        ((cerr << (first ? "" : ","), __print(args), first = false), ...);
      },
      t);
  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...);
}
#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \
  cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = [";              \
  _print(__VA_ARGS__)
#else
#define dbg(...)
#endif

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;
  if (n == 1) {
    cout << "YES";
    return;
  }
  if (a[0] > a[1]) {
    cout << "NO";
    return;
  }
  int i = 0;
  while (i + 1 < n && a[i] == a[i + 1])
    i++;
  if (i + 1 < n && a[i] > a[i + 1]) {
    cout << "NO";
    return;
  }
  cout << "YES";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int T;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
    cout << '\n';
  }
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define all(x) (x).begin(), (x).end()
const ll INF = 1000000000000000000;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
string stepDir = "URDL";

/******** 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... A> void __print(const tuple<A...> &t);
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... A> void __print(const tuple<A...> &t) {
  bool first = true;
  cerr << '(';
  apply(
      [&first](const auto &...args) {
        ((cerr << (first ? "" : ","), __print(args), first = false), ...);
      },
      t);
  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...);
}
#ifndef ONLINE_JUDGE
#define dbg(...)                                                               \
  cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = [";              \
  _print(__VA_ARGS__)
#else
#define dbg(...)
#endif

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;
  if (n == 1) {
    cout << "YES";
    return;
  }
  if (a[0] > a[1]) {
    cout << "NO";
    return;
  }
  int i = 0;
  while (i + 1 < n && a[i] <= a[i + 1]) {
    a[i + 1] -= a[i];
    a[i] = 0;
    i++;
  }
  if (i + 1 < n && a[i] > a[i + 1]) {
    cout << "NO";
    return;
  }
  dbg(a);
  cout << "YES";
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int T;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
    cout << '\n';
  }
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
void solve(){
    int n;
        cin >>n;
        int a[n];
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        for(int i=0; i<n-1; i++){
            if(a[i]>a[i+1]){
                cout << "NO\n";
                return;
            }else{
                a[i+1]-=min(a[i],a[i+1]);
                a[i]-=min(a[i],a[i+1]);
            }
        }
        cout << "YES\n";
}
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int t;
    cin >> t;
    while(t--){
        solve();}
        return 0;
}

#include <bits/stdc++.h>
using namespace std;
void solve(){
    int n;
        cin >>n;
        int a[n];
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        for(int i=0; i<n-1; i++){
            if(a[i]>a[i+1]){
                cout << "NO\n";
                return;
            }else{
                a[i+1]-=min(a[i],a[i+1]);
                a[i]-=min(a[i],a[i+1]);
            }
        }
        cout << "YES\n";
}
int main(){
    int t;
    cin >> t;
    while(t--){
        solve();}
        return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int a[N];
int n;
void solve(){
	a[0]=0;
	for(int i=1;i<n;i++){
		int x=min(a[i],a[i+1]);
		a[i]-=x;
		a[i+1]-=x;
		if(a[i]<a[i-1]){
			cout<<"NO"<<endl;
			return ;
		}
	}
	cout<<"YES"<<endl;
}
signed main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		solve();
	}

}

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int a[N];
int n;
void solve(){
	a[0]=0;
	for(int i=1;i<n;i++){
		int x=min(a[i],a[i+1]);
		a[i]-=x;
		a[i+1]-=x;
		if(a[i]<a[i-1]){
			cout<<"NO"<<endl;
			return ;
		}
	}
	for(int i=1;i<n;i++){
		if(a[i]>a[i+1]){
			cout<<"NO"<<endl;
			return ;
		}
	}
	cout<<"YES"<<endl;
}
signed main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		solve();
	}

}

Answer: 2


#include <iostream>

#include <vector>

using namespace std;

void solve()

{

    int n;

    cin >> n;

    vector<int> a(n);

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

    {

        cin >> a[i];

    }

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

    {

        if (a[i] > a[i + 1])

        {

            int diff = a[i] - a[i + 1];

            a[i] -= diff;  // Reduce both a[i] and a[i+1] by 'diff'

            a[i + 1] -= diff;

        }

        if (a[i] > a[i + 1])

        {

            cout << "NO" << endl;

            return;

        }

    }

    cout << "YES" << endl;

}

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

void solve() {

    ll n;

    cin >> n;

    vector<ll> a(n);

    for (ll &x : a) cin >> x;

    ll last = 0;

    // Find the last position where a[i] > a[i+1]

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

        if (a[i - 1] > a[i]) last = i;

    }

    // If already sorted, return YES

    if (last == 0) {

        cout << "YES\n";

        return;

    }

    // Apply the operation

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

        ll min_val = min(a[i], a[i + 1]);

        a[i] -= min_val;

        a[i + 1] -= min_val;

    }

    // Check if sorted after operations

    cout << (is_sorted(a.begin(), a.end()) ? "YES" : "NO") << '\n';

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    ll t;

    cin >> t;

    while (t--) solve();

    return 0;

}

Answer: 2


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

int sum(vector<int> &a, int n)
{
    int x = 0;
    for (int i = 0; i < n; i++)
    {
        x += abs(a[i]);
    }
    return x;
}

void solve()
{
   int n; cin>>n;
   vector<int>a(n);
   for(int &x : a) cin>>x;
   int g = 0, f = 0;
   for(int i=1;i<n;i++){
    if(a[i] == a[i-1]) continue;
    if(a[i] < a[i-1] && !g) f = 1;
    if(a[i] > a[i-1]) g = 1;
   }
   if(f==0) cout<<"YES\n";
   else cout<<"NO\n";
}

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

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

int sum(vector<int> &a, int n)
{
    int x = 0;
    for (int i = 0; i < n; i++)
    {
        x += abs(a[i]);
    }
    return x;
}

void solve()
{
   int n; cin>>n;
   vector<int>a(n);
   for(int &x : a) cin>>x;
   int g = 0, f = 0;
   for(int i=1;i<n;i++){
        int x = min(a[i],a[i-1]);
        a[i] -= x;
        a[i-1] -= x;
        if(0 == a[i] && a[i-1] != 0) {f = 1; break;}
   }
   if(f==0) cout<<"YES\n";
   else cout<<"NO\n";
}

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

Answer: 2


#include<bits/stdc++.h>

using namespace std;

void opp(int &i,int &j){

int k=min(i,j);

i-=k;

j-=k;

}

int can3be(vector<int> &v,int i){

int i1=v[i],j1=v[i+1],k1=v[i+2];

int i2=v[i],j2=v[i+1],k2=v[i+2];

opp(i1,j1);opp(j1,k1);

opp(j2,k2);opp(i2,j2);

if(i1<=j1&&j1<=k1){

opp(v[i],v[i+1]);opp(v[i+1],v[i+2]);

return 1;

}

if(i2<=j2&&j2<=k2){

opp(v[i+1],v[i+2]);opp(v[i],v[i+1]);

return 1;

}

return 0;

}

int main(){

ios_base::sync_with_stdio(0);cin.tie(0);

int t;

cin>>t;

while(t--){

int n;

cin>>n;

vector<int> v(n);

int flag=1;

for(auto &i:v) cin>>i;

if(n==2){

if(v[0]<=v[1]) cout<<"YES"<<"\n";

else cout<<"NO"<<"\n";

}

else{

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

flag = can3be(v,i);

if(flag==0) break;

}

if(flag) cout<<"YES"<<"\n";

else cout<<"NO"<<"\n";

}

}

}

#include<bits/stdc++.h>

using namespace std;

void opp(int &i,int &j){

int k=min(i,j);

i-=k;

j-=k;

}

int can3be(vector<int> &v,int i){

int i1=v[i],j1=v[i+1],k1=v[i+2];

int i2=v[i],j2=v[i+1],k2=v[i+2];

opp(i1,j1);opp(j1,k1);

opp(j2,k2);opp(i2,j2);

if(i1<=j1&&j1<=k1){

opp(v[i],v[i+1]);opp(v[i+1],v[i+2]);

return 1;

}

if(i2<=j2&&j2<=k2){

opp(v[i+1],v[i+2]);opp(v[i],v[i+1]);

return 1;

}

return 0;

}

int main(){

ios_base::sync_with_stdio(0);cin.tie(0);

int t;

cin>>t;

while(t--){

int n;

cin>>n;

vector<int> v(n);

int flag=1;

for(auto &i:v) cin>>i;

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

flag = can3be(v,i);

if(flag==0) break;

}

if(flag) cout<<"YES"<<"\n";

else cout<<"NO"<<"\n";

}

}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define gcd(a,b) __gcd(a,b)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pub push_back
#define pob pop_back

#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll//TO FIND NUMBER OF SET BITS __builtin_popcountll(n);-->>set_bits(n);
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
ll lcm(ll a,ll b)
{
return (a*b)/__gcd(a,b);
}
ll LOWERBOUND(vector<ll> &vp,ll n,ll search)
{
ll ans=-1;
ll l=0,r=n-1;
while(l<=r)
{
ll mid=l+(r-l)/2;
if(vp[mid]<=search)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
return ans+1;

}
ll UPPERBOUND(vector<ll> &vp,ll n,ll search)
{
ll ans=-1;
ll l=0,r=n-1;
while(l<=r)
{
ll mid=l+(r-l)/2;
if(vp[mid]>=search)
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
return ans+1;

}
int ispal(string &s)
{
string s1=s;
reverse(s1.begin(),s1.end());
if(s1==s)
return 1;
return 0;
}
void div(ll &a,ll &b,ll index1,ll index2,vector<pair<ll,ll>> &ans)
{
while(a!=b&&(a!=2||b!=2))
{
if(a<b)
{
b=(a+b-1)/a;
ans.push_back({index2,index1});
}
else
{
a=(a+b-1)/b;
ans.push_back({index1,index2});
}
}
}
ll mex(vector<ll> vp)
{
sort(vp.begin(),vp.end());
ll mex1=0;
ll n=vp.size();
for(ll ind=0;ind<n;ind++)
{
if(vp[ind]==mex1)
{
mex1+=1;
}

}
return mex1;
}
ll log_a_to_base_b(ll a, ll b)
{
return log2(a) / log2(b);
}

ll toggleKthBit(ll n, int k)
{
//0-th indexed
return (n ^ (1ll << (k)));
}
bool kthbitset(ll n,int k)
{
return ((n>>k)&1);
}
bool iscomp(ll n){
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 1;
}
return 0;

}

int compute(int x,int y,set<pair<int,int>> &vis,vector<vector<char>> &a,int n,int m){

if( x<0||y<0||x>=n||y>=m)
return 0;
if(vis.find({x,y})!=vis.end())
{
return 0;
}
if(a[x][y]!='?')
{
char move=a[x][y];
vis.insert({x,y});
if(move=='U')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='R')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='D')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='L')
{
return 1+compute(x-1,y,vis,a,m,n);
}
}
else
{
//when we go up

//when we go right

//when we go down

//when we go left

}

}

void dfs(int flag,ll val,map<ll,vector<ll>> &m,set<ll> &s1,set<ll> &s2,int n,bool done[])
{
if(done[val]){
return;
}
done[val]=1;
if(flag==0)
{
if(s1.find(val)==s1.end())
{
s1.insert(val);
for(auto it:m[val])
{
dfs(1,it,m,s1,s2,n,done);
}
}

}
else
{
if(s2.find(val)==s2.end())
{
s2.insert(val);
for(auto it:m[val])
{
dfs(0,it,m,s1,s2,n,done);
}
}
}

}

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

 sort(a,a+n);
 sort(b,b+m);
 ll count=n;
 map<ll,ll> mp;
 for(auto it:a){
   mp[it]++;
 }

 multiset<ll> s;
 for(int i=0;i<m;i++){
   if(mp[b[i]]){
      mp[b[i]]--;
      count--;
   }
   else{
      s.insert(b[i]);
   }
 }
 //cout<<(n-count)+(s.size())<<nline;
 while(s.size() && count&& (n-count)+(s.size())<=n){
      //return;
      auto it=s.find(*s.rbegin());
      ll num=*it;
      if(mp[num]){
         mp[num]--;
         count--;
         s.erase(it);
      }
      else{
         s.insert((num+1)/2);
         s.insert(num/2);
         s.erase(num);

      }
}
//cout<<count<<nline;
if(count==0){
   cout<<"Yes\n";
}
else
cout<<"No\n";

}
int main()
{

ios_base::sync_with_stdio(0);
cin.tie(0);

#ifndef ONLINE_JUDGE
//for getting input from input.txt
freopen("input.txt","r",stdin);
//for getting output from output.txt
freopen("output.txt","w",stdout);
#endif
//generatenos("",0,5);
int t=1;

cin>>t;

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

}

#include<bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define gcd(a,b) __gcd(a,b)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pub push_back
#define pob pop_back

#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll//TO FIND NUMBER OF SET BITS __builtin_popcountll(n);-->>set_bits(n);
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
ll lcm(ll a,ll b)
{
return (a*b)/__gcd(a,b);
}
ll LOWERBOUND(vector<ll> &vp,ll n,ll search)
{
ll ans=-1;
ll l=0,r=n-1;
while(l<=r)
{
ll mid=l+(r-l)/2;
if(vp[mid]<=search)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
return ans+1;

}
ll UPPERBOUND(vector<ll> &vp,ll n,ll search)
{
ll ans=-1;
ll l=0,r=n-1;
while(l<=r)
{
ll mid=l+(r-l)/2;
if(vp[mid]>=search)
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
return ans+1;

}
int ispal(string &s)
{
string s1=s;
reverse(s1.begin(),s1.end());
if(s1==s)
return 1;
return 0;
}
void div(ll &a,ll &b,ll index1,ll index2,vector<pair<ll,ll>> &ans)
{
while(a!=b&&(a!=2||b!=2))
{
if(a<b)
{
b=(a+b-1)/a;
ans.push_back({index2,index1});
}
else
{
a=(a+b-1)/b;
ans.push_back({index1,index2});
}
}
}
ll mex(vector<ll> vp)
{
sort(vp.begin(),vp.end());
ll mex1=0;
ll n=vp.size();
for(ll ind=0;ind<n;ind++)
{
if(vp[ind]==mex1)
{
mex1+=1;
}

}
return mex1;
}
ll log_a_to_base_b(ll a, ll b)
{
return log2(a) / log2(b);
}

ll toggleKthBit(ll n, int k)
{
//0-th indexed
return (n ^ (1ll << (k)));
}
bool kthbitset(ll n,int k)
{
return ((n>>k)&1);
}
bool iscomp(ll n){
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 1;
}
return 0;

}

int compute(int x,int y,set<pair<int,int>> &vis,vector<vector<char>> &a,int n,int m){

if( x<0||y<0||x>=n||y>=m)
return 0;
if(vis.find({x,y})!=vis.end())
{
return 0;
}
if(a[x][y]!='?')
{
char move=a[x][y];
vis.insert({x,y});
if(move=='U')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='R')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='D')
{
return 1+compute(x-1,y,vis,a,m,n);
}
else if(move=='L')
{
return 1+compute(x-1,y,vis,a,m,n);
}
}
else
{
//when we go up

//when we go right

//when we go down

//when we go left

}

}

void dfs(int flag,ll val,map<ll,vector<ll>> &m,set<ll> &s1,set<ll> &s2,int n,bool done[])
{
if(done[val]){
return;
}
done[val]=1;
if(flag==0)
{
if(s1.find(val)==s1.end())
{
s1.insert(val);
for(auto it:m[val])
{
dfs(1,it,m,s1,s2,n,done);
}
}

}
else
{
if(s2.find(val)==s2.end())
{
s2.insert(val);
for(auto it:m[val])
{
dfs(0,it,m,s1,s2,n,done);
}
}
}

}

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

 sort(a,a+n);
 sort(b,b+m);
 ll count=n;
 map<ll,ll> mp;
 for(auto it:a){
   mp[it]++;
 }

 multiset<ll> s;
 for(int i=0;i<m;i++){
   if(mp[b[i]]){
      mp[b[i]]--;
      count--;
   }
   else{
      s.insert(b[i]);
   }
 }
 //cout<<(n-count)+(s.size())<<nline;
 while(s.size() && count&& (n-count)+(s.size())<=n){
      //return;
      auto it=s.find(*s.rbegin());
      ll num=*it;
      if(mp[num]){
         mp[num]--;
         count--;
         s.erase(it);
      }
      else{
         s.insert((num+1)/2);
         s.insert(num/2);
         s.erase(it);

      }
}
//cout<<count<<nline;
if(count==0 && s.size()==0 ){
   cout<<"Yes\n";
}
else
cout<<"No\n";

}
int main()
{

ios_base::sync_with_stdio(0);
cin.tie(0);

#ifndef ONLINE_JUDGE
//for getting input from input.txt
freopen("input.txt","r",stdin);
//for getting output from output.txt
freopen("output.txt","w",stdout);
#endif
//generatenos("",0,5);
int t=1;

cin>>t;

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

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int MOD = 1000000007;
#define rep(a,b,c) for(int a = b; a < c; ++a)
#define repr(a,b,c) for(int a = b; a > c; --a)

void solve (int test_case) {
    // the key is to visualize
    // to get better, work on speed - https://www.youtube.com/watch?v=BqWqzvnbnjU
    int n, m;
    cin >> n >> m;
    int a [n], b [m];
    rep (i,0,n) cin >> a[i];
    rep (i,0,m) cin >> b[i];
    multiset<int> ms;
    rep (i,0,n) ms.insert(a[i]);
    map<int, int> mp;
    rep (i,0,m) mp[b[i]] += 1;
    while (ms.size() > 1) {
        if (mp.size() == 0) {
            cout << "NO\n";
            return;
        }
        int x = *ms.begin();
        ms.erase(ms.begin());
        if (mp.find(x) != mp.end()) {
            mp[x] -= 1;
            if (mp[x] == 0) mp.erase(x);
            continue;
        }
        int y = *ms.begin();
        ms.erase(ms.begin());
        if (y - x > 1) {
            cout << "NO\n";
            return;
        }
        int z = x + y;
        if (mp.find(z) != mp.end()) {
            mp[z] -= 1;
            if (mp[z] == 0) mp.erase(z);
        }
        else {
            ms.insert(z);
        }
    }
    if (ms.size()) {
        if (mp.size() == 0) {
            cout << "NO\n";
            return;
        }
       int x = *ms.begin();
        ms.erase(ms.begin());
        if (mp.find(x) != mp.end()) {
            mp[x] -= 1;
            if (mp[x] == 0) mp.erase(x);
        }
        else {
            cout << "NO\n";
            return;
        }
    }
    if (mp.size()) cout << "NO\n";
    else cout << "YES\n";
}

int32_t main () {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int test_cases = 1;
    cin >> test_cases;
    for (int i = 1; i <= test_cases; ++i) solve(i);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int MOD = 1000000007;
#define rep(a,b,c) for(int a = b; a < c; ++a)
#define repr(a,b,c) for(int a = b; a > c; --a)

void solve (int test_case) {
    // the key is to visualize
    // to get better, work on speed - https://www.youtube.com/watch?v=BqWqzvnbnjU
    int n, m;
    cin >> n >> m;
    int a [n], b [m];
    rep (i,0,n) cin >> a[i];
    rep (i,0,m) cin >> b[i];
    map<int, int> mp;
    int cnt = 0;
    rep (i,0,n) mp[a[i]] += 1, cnt += 1;
    priority_queue<int> pq;
    rep (i,0,m) pq.push(b[i]);
    while (pq.size() && pq.size() <= cnt) {
        int x = pq.top();
        pq.pop();
        if (mp.find(x) != mp.end()) {
            mp[x] -= 1;
            if (mp[x] == 0) {
                mp.erase(x);
            }
            cnt -= 1;
        }
        else {
            pq.push(x / 2);
            pq.push(x - x / 2);
        }
    }
    if (mp.size()) cout << "NO\n";
    else cout << "YES\n";
}

int32_t main () {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int test_cases = 1;
    cin >> test_cases;
    for (int i = 1; i <= test_cases; ++i) solve(i);
    return 0;
}

Answer: 2