Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
// #pragma GCC target("avx")

// #pragma GCC optimize(3,"Ofast","inline","-ffast-math")

// #pragma pack(1) // 修改内存对齐



#include <iostream>

#include <vector>

#include <string.h>

#include <unordered_map>

#include <unordered_set>

#include <queue>

#include <set>

#include <map>

#include <iomanip>

#include <memory>

#include <stack>

#include <cmath>

#include <algorithm>

#include <functional>

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

#include <regex>

#include <climits>



#define x first

#define y second

#define pb push_back

#define ppc __builtin_popcount

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

#define fd(i,a,b) for(int i=a;i>=b; -- i)

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

#define endl '\n'

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



// #define int long long

using namespace std;



using LL =long long ;

using LD =long double ;

using ULL = unsigned long long;

using VVL =  vector<vector<LL>>;

using VVI =  vector<vector<int>>;

using VL = vector<LL>;

using VB = vector<bool>;

using VI = vector<int>;

using VS = vector<string>;

using PII = pair<int,int>;

using VPII = vector<PII>;

using PIII = pair<int,PII>;

using PDD =  pair<double,double>;

using PDI = pair<double,int>;

using PCI = pair<char,int>;

using PCS = pair<char,string>;

using PSI = pair<string,int> ;

using PIS = pair<int,string>;

using PIC = pair<int,char>;

using PLL = pair<LL,LL>;

using PLI = pair<LL,int>;

using i128 = __int128;

using u128 = unsigned __int128;



namespace ck

{

#define ms(x,y) memset(x,y,sizeof x)

// 获取可变参数的个数

template <typename ...Args>

int count(const Args& ...args)

{

return sizeof ...(args);

}

// 使用可变参数构造 tuple

template <typename ...Args>

auto make_tuple(const Args& ...args)

{

return make_tuple(args...);

}

// 使用可变参数构造数组

template <typename ...Args>

auto make_array(const Args& ...args)

{

return array{args...};

}

// 得到x向上取整 ,y的整数倍

template<class T>

T get_round_up(T x,T y)

{

return (x + y -1 ) & ~ (y-1);

}



template<class T>

inline T qmi(T a,T b,T k)

{

T res = T(1);

while(b)

{

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

a = 1ull * (a * a ) % k ;

b >>= 1;

}

return res;

}



template<class T>

inline T lowbit(T x)

{

return x & -x ;

}

}// end of namespace ck



// using namespace ck;

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



void  read_line(){

string s;

stringstream ss;

while(getline(cin,s)){

stringstream ss(s);

string a;

while(ss >> a ) cout << a << endl;

}

}



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



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

const double eps = 1e-8 ;

void init (int n);

void solve();

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

signed main()

{

// system("chcp 65001");// 设置控制台编码为UTF-8

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

// init(N);

ios

//  cout << fixed<<setprecision(2);

int t=1;

cin>>t;

// int now = 1;

while(t -- )

{

//     cout<<"Case ";

//      cout<<"Case #";

//      cout<<"Scenario #";

//     cout<< now ++ <<": ";

//      cout<< now ++ <<": \n";

solve();

}





return 0;

}

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



static int INIT = [](int n = N -1){

return 0;

}();





void solve(){

int n ,m ; cin >> n;

vector<int> a(n);

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



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

cout <<"NO" << endl;

return;

}



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

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

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

}





// for(auto t : a) cout << t << " ";cout<< endl;



bool flag=0 ;

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

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

flag = 1; break;

}

if(flag) cout << "NO" << endl;

else cout << "YES" << endl;

// cout << endl;

}





Submission (verdict: Accepted):
// #pragma GCC target("avx")

// #pragma GCC optimize(3,"Ofast","inline","-ffast-math")

// #pragma pack(1) // 修改内存对齐



#include <iostream>

#include <vector>

#include <string.h>

#include <unordered_map>

#include <unordered_set>

#include <queue>

#include <set>

#include <map>

#include <iomanip>

#include <memory>

#include <stack>

#include <cmath>

#include <algorithm>

#include <functional>

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

#include <regex>

#include <climits>



#define x first

#define y second

#define pb push_back

#define ppc __builtin_popcount

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

#define fd(i,a,b) for(int i=a;i>=b; -- i)

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

#define endl '\n'

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



// #define int long long

using namespace std;



using LL =long long ;

using LD =long double ;

using ULL = unsigned long long;

using VVL =  vector<vector<LL>>;

using VVI =  vector<vector<int>>;

using VL = vector<LL>;

using VB = vector<bool>;

using VI = vector<int>;

using VS = vector<string>;

using PII = pair<int,int>;

using VPII = vector<PII>;

using PIII = pair<int,PII>;

using PDD =  pair<double,double>;

using PDI = pair<double,int>;

using PCI = pair<char,int>;

using PCS = pair<char,string>;

using PSI = pair<string,int> ;

using PIS = pair<int,string>;

using PIC = pair<int,char>;

using PLL = pair<LL,LL>;

using PLI = pair<LL,int>;

using i128 = __int128;

using u128 = unsigned __int128;



namespace ck

{

#define ms(x,y) memset(x,y,sizeof x)

// 获取可变参数的个数

template <typename ...Args>

int count(const Args& ...args)

{

return sizeof ...(args);

}

// 使用可变参数构造 tuple

template <typename ...Args>

auto make_tuple(const Args& ...args)

{

return make_tuple(args...);

}

// 使用可变参数构造数组

template <typename ...Args>

auto make_array(const Args& ...args)

{

return array{args...};

}

// 得到x向上取整 ,y的整数倍

template<class T>

T get_round_up(T x,T y)

{

return (x + y -1 ) & ~ (y-1);

}



template<class T>

inline T qmi(T a,T b,T k)

{

T res = T(1);

while(b)

{

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

a = 1ull * (a * a ) % k ;

b >>= 1;

}

return res;

}



template<class T>

inline T lowbit(T x)

{

return x & -x ;

}

}// end of namespace ck



// using namespace ck;

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



void  read_line(){

string s;

stringstream ss;

while(getline(cin,s)){

stringstream ss(s);

string a;

while(ss >> a ) cout << a << endl;

}

}



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



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

const double eps = 1e-8 ;

void init (int n);

void solve();

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

signed main()

{

// system("chcp 65001");// 设置控制台编码为UTF-8

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

// init(N);

ios

//  cout << fixed<<setprecision(2);

int t=1;

cin>>t;

// int now = 1;

while(t -- )

{

//     cout<<"Case ";

//      cout<<"Case #";

//      cout<<"Scenario #";

//     cout<< now ++ <<": ";

//      cout<< now ++ <<": \n";

solve();

}





return 0;

}

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



static int INIT = [](int n = N -1){

return 0;

}();





void solve(){

int n ,m ; cin >> n;

vector<int> a(n);

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

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

cout <<"NO" << endl;

return;

}

vector<int> b= a;

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

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

b[i + 1 ] -= b[i]; b[i] =0 ;

}



bool flag=1 ;

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

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

flag = 0; break;

}

if(flag) {

cout << "YES" << endl;

return ;

}



b=  a;

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

b[0] -= t ; b[1] -= t ;

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

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

b[i + 1 ] -= b[i]; b[i] =0 ;

}



flag=1 ;

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

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

flag = 0; break;

}



if(flag) {

cout << "YES" << endl;

return ;

}



b= a;

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

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

b[i] -= t ; b[i + 1] -= t ;

}

flag=1 ;

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

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

flag = 0; break;

}



if(flag) cout <<"YES" << endl;

else cout<< "NO" << endl;

}