Help me in solving DISJOINTSUB problem

My issue

Explain your approach

My code

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

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];
        }
        bool flag = true;
        int maxi=0;
        
        for (int i = 0; i < n - 1; i++) {
            if (a[i] > a[i + 1]) {
             maxi=max(maxi,(a[i]-a[i+1]));
        }
        for(int i=0;i<n;i++)
        {
            if(a[i]>a[i+1])
            {
                a[i+1]=a[i+1]+maxi;
            }
        }
        for(int i=0;i<n-1;i=i+2)
        {
            if(a[i]>a[i+1])
            {
                flag=0;
            }
        }
        if (flag) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Problem Link: Disjoint Non-Decreasing Array Practice Coding Problem - CodeChef

I think your approach and logic here is wrong . If you want some hints , the first one is to use sorting somehow

here is what I did to solve this : I made a visited boolean array at first . if I see a number , I turn the visited True and if the number is already visited and True , I add it to a list with the name of over_visited array . now after one iteration I make a new array of the numebrs that I havent seen . now I sort both of these arrays and the sum of differences between each element of these arrays is the solution . because you are giving every number that is not seen to the closest one from the input array .