stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

 

#define ll long long int

#define pb push_back

#define mp make_pair

#define ff first

#define ss second

#define lim 200005

#define mod 1000000007

#define nfs_mw ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;



bool Q;

struct Line {

	mutable ll k, m, p;

	bool operator<(const Line& o) const {

		return Q ? p < o.p : k < o.k;

	}

};



//Line equation is k*x + m

//works for maximum, negate values for minimum

struct LineContainer : multiset<Line> {

	// (for doubles, use inf = 1/.0, div(a,b) = a/b)

	const ll inf = LLONG_MAX;

	ll div(ll a, ll b) { // floored division

		return a / b - ((a ^ b) < 0 && a % b); }

	bool isect(iterator x, iterator y) {

		if (y == end()) { x->p = inf; return false; }

		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;

		else x->p = div(y->m - x->m, x->k - y->k);

		return x->p >= y->p;

	}

	void add(ll k, ll m) {

		auto z = insert({k, m, 0}), y = z++, x = y;

		while (isect(y, z)) z = erase(z);

		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));

		while ((y = x) != begin() && (--x)->p >= y->p)

			isect(x, erase(y));

	}

	ll query(ll x) {

		assert(!empty());

		Q = 1; auto l = *lower_bound({0,0,x}); Q = 0;

		return l.k * x + l.m;

	}

	void min_add(ll k, ll m) {

		add(-k, -m);

	}

	ll min_query(ll x) {

		return -query(x);

	}

};







int main()

{

    nfs_mw;

    int i,j,n; ll c, h, dp[lim] = {};

    cin>>n>>c;

    LineContainer idk;

    dp[0] = 0; cin>>h;

    idk.min_add(-2*h, dp[0] + h*h + c);

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

    	cin>>h;

    	dp[i] = idk.min_query(h) + h*h;

    	idk.min_add(-2*h, dp[i] + h*h + c);

    }

    cout<<dp[n-1];

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define ull uint64_t

#define ll long long int

#define pb push_back

#define mp make_pair

#define ff first

#define ss second

#define mx5 100005

#define mx52 200005

#define mx6 1000005

#define mod 1000000007

#define nfs ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;



struct Line {

    ll m, c;

    double intersect(const Line& b) {

        return ((double)(c - b.c)) / (b.m - m);

    }



    ll eval(int x) {

        return m*x + c;

    }

};



Line getLine(ll dpi, ll hi) {

    return {-2*hi, hi*hi + dpi};

}



int main() {

    nfs

    int i, j, n;

    ll c, h[mx52];

    

    cin >> n >> c;

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

        cin >> h[i];

    }

    

    ll dp[n] = {};

    deque <Line> options(1, getLine(dp[0], h[0]));

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

        while (options.size() > 1 && options[0].intersect(options[1]) < h[i])

            options.pop_front();

        dp[i] = options[0].eval(h[i]) + h[i]*h[i] + c;

        Line newLine = getLine(dp[i], h[i]);

        int li = options.size() - 1;

        while (li and options[li].intersect(options[li - 1]) >= options[li].intersect(newLine)) {

            options.pop_back();

            li--;

        }

        options.push_back(newLine);

    }

    cout << dp[n - 1];

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

#include <cstdio>



using namespace std;



using ll = long long;

struct Line { ll a, b; ll get(ll x) { return a * x + b; } };



static const int MAX = 2e5 + 9;

const ll INFL = 1e17;



struct LiChaoTree

{

	int N;

    Line a[MAX << 2];

    ll h[MAX];



    void init (ll _h[MAX], int _N)

    {

    	N = _N;

    	copy (_h, _h + N, h);

    	for (int i = 0; i < (N << 2); ++i) a[i] = {0, INFL};

    }



    void upd (Line ln)

    {

        int root = 1, lo = 0, hi = N;

        while (true) {

			int mid = (lo + hi) >> 1;

            if (ln.get(h[mid]) < a[root].get(h[mid])) swap (ln, a[root]);

//            cerr << '[' << h[lo] << ", " << h[hi-1] << "]: " << a[root].a << ' ' << a[root].b << endl;

            if (lo + 1 >= hi) return;

            if (ln.get(h[lo]) < a[root].get(h[lo])) root = root << 1, hi = mid;

            else root = root << 1 | 1, lo = mid;

        }

    }



    ll get (int x)

    {

        int root = 1, lo = 0, hi = N; ll ans = INFL;

        while (true) {

        	int mid = (lo + hi) >> 1;

            ans = min(ans, a[root].get(h[x]));

            if (lo + 1 >= hi) return ans;

            if (x < mid) root = root << 1, hi = mid;

            else root = root << 1 | 1, lo = mid;

        }

    }



} tree;



int N;

ll C, h[MAX], f;



int main()

{

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

    cin >> N >> C;

    for (int i = 0; i < N; ++i) cin >> h[i];

    tree.init(h, N);



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

        f = (i == 0) ? 0ll : (tree.get(i) + h[i] * h[i] + C);

        if (i == N-1) return cout << f << endl, 0;

//        cout << f << ' ';

        tree.upd({-2 * h[i], f + h[i] * h[i]});

    }

}


// Optimized solution to the same problem (faster):
#include <utility>

#include <algorithm>

#include <cstdio>



using namespace std;

using ll = long long;

using pll = pair<ll, ll>;

#define a first

#define b second



static const int MAXN = 200100;



static pll pnts[MAXN];

struct CHT

{

	int ptr = 0, sz = 0;



	bool bad(pll p0, pll p1, pll p2) { return (p1.a - p0.a) * (p2.b - p0.b) - (p2.a - p0.a) * (p1.b - p0.b) > 0; }

	void add(pll p)

	{

		while (sz > 1 && bad(pnts[sz - 2], pnts[sz - 1], p)) --sz;

		pnts[sz++] = p;

	}

	ll val(int i, ll x) { return pnts[i].a * x + pnts[i].b; }

	ll get(ll x)

	{

		ptr = min(ptr, sz - 1);

		while (ptr < sz - 1 && val(ptr, x) > val(ptr + 1, x)) ++ptr;

		return val(ptr, x);

	}

} cht;



int N;

ll h, c, f;



int main()

{

	scanf("%d %lld", &N, &c);

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

		scanf("%lld", &h);

		f = (i == 0 ? 0 : cht.get(h) + h * h + c);

		cht.add(pll(-2 * h, h * h + f));

	}

	printf("%lld\n", f);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

#include <cstdio>



using namespace std;



using ll = long long;

struct Line { ll a, b; ll get(ll x) { return a * x + b; } };



static const int MAX = 2e5 + 9;

const ll INFL = 1e17;



struct LiChaoTree

{

	int N;

    Line a[MAX << 2];

    ll h[MAX];



    void init (ll _h[MAX], int _N)

    {

    	N = _N;

    	copy (_h, _h + N, h);

    	for (int i = 0; i < N << 2; ++i) a[i] = {0, INFL};

    }



    void upd (Line ln)

    {

        int root = 1, lo = 0, hi = N;

        while (true) {

			int mid = lo + hi >> 1;

            if (ln.get(h[mid]) < a[root].get(h[mid])) swap (ln, a[root]);

//            cerr << '[' << h[lo] << ", " << h[hi-1] << "]: " << a[root].a << ' ' << a[root].b << endl;

            if (lo + 1 >= hi) return;

            if (ln.get(h[lo]) < a[root].get(h[lo])) root = root << 1, hi = mid;

            else root = root << 1 | 1, lo = mid;

        }

    }



    ll get (int x)

    {

        int root = 1, lo = 0, hi = N; ll ans = INFL;

        while (true) {

        	int mid = lo + hi >> 1;

            ans = min(ans, a[root].get(h[x]));

            if (lo + 1 >= hi) return ans;

            if (x < mid) root = root << 1, hi = mid;

            else root = root << 1 | 1, lo = mid;

        }

    }



} tree;



int N;

ll C, h[MAX], f;



int main()

{

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

    cin >> N >> C;

    for (int i = 0; i < N; ++i) cin >> h[i];

    tree.init(h, N);



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

        f = (i == 0) ? 0ll : (tree.get(i) + h[i] * h[i] + C);

        if (i == N-1) return cout << f << endl, 0;

//        cout << f << ' ';

        tree.upd({-2 * h[i], f + h[i] * h[i]});

    }

}


// Optimized solution to the same problem (faster):
#include <utility>

#include <algorithm>

#include <cstdio>



using namespace std;

using ll = long long;

using pll = pair<ll, ll>;

#define a first

#define b second



static const int MAXN = 200100;



static pll pnts[MAXN];

struct CHT

{

	int ptr = 0, sz = 0;



	bool bad(pll p0, pll p1, pll p2) { return (p1.a - p0.a) * (p2.b - p0.b) - (p2.a - p0.a) * (p1.b - p0.b) > 0; }

	void add(pll p)

	{

		while (sz > 1 && bad(pnts[sz - 2], pnts[sz - 1], p)) --sz;

		pnts[sz++] = p;

	}

	ll val(int i, ll x) { return pnts[i].a * x + pnts[i].b; }

	ll get(ll x)

	{

		ptr = min(ptr, sz - 1);

		while (ptr < sz - 1 && val(ptr, x) > val(ptr + 1, x)) ++ptr;

		return val(ptr, x);

	}

} cht;



int N;

ll h, c, f;



int main()

{

	scanf("%d %lld", &N, &c);

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

		scanf("%lld", &h);

		f = (i == 0 ? 0 : cht.get(h) + h * h + c);

		cht.add(pll(-2 * h, h * h + f));

	}

	printf("%lld\n", f);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long int

using namespace std;



int main()

{

    ll n,a[100000+9],b[100000+9]= {0},i,j,f=0,c=0;

    cin>>n;

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

    {

        cin>>a[i];

        b[i]=a[i];

    }

    sort(a,a+n);

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

    {

        if(a[i]!=b[i])

        {

            f=1;

            c++;

        }

    }



    if(f==0)

    {

        cout<<"YES"<<endl;

    }

    else if(f==1)

    {

        if(c==2)

        {

            cout<<"YES"<<endl;

        }

        else

        {

            cout<<"NO"<<endl;

        }

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

int main()

{

    ll n,i,j,k,l,a,b,c,d;

    cin>>n;

    ll arr[n+10];

    c=0;

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

    {

        cin>>arr[i];

    }

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

    {

        if(arr[i]!=i)

        {

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

            {

                if(arr[j]==i)

                {

                    arr[j]=arr[i];

                    arr[i]=i;

                    break;

                    break;



                }

            }

            break;

        }

    }

    c=-1;

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

    {

        if(arr[i]==i){c=1;}

        else{c=0;break;}

    }

    if(c==1)

    {

        cout<<"YES"<<endl;

    }

    else if(c==0)

    {

        cout<<"NO"<<endl;

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):


///HELLO THERE~!

///CREATED BY MD SADMAN MEHEDI SIVAN(IUT CSE[SWE'19])



#include<bits/stdc++.h>

using namespace std;



#define FASTIO          ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

#define READ            freopen("input.txt","r",stdin);

#define WRITE           freopen("red.txt","w",stdout);



using ll=long long;

using ull=unsigned long long;

const double pi = 2*acos(0.0);

const long long MX = 2e5+7;



int main()

{

    FASTIO;

    long long n;

    cin>>n;

    long long a[n+1];

    ll f=0;

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

    {

        cin>>a[i];

        if(a[i]!=i+1)

            f++;

    }

    if(f==0 || f==2)

    {

        cout<<"YES";

    }

    else

        cout<<"NO";







    return 0;







}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



int main()



{

   long long a=0,b,c,d,e,f,g,h,i,j,k,l,m,n,t;



   cin>>n;



   long long ar[n];



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

   {

       cin>>ar[i];



       if(ar[i]!=i) a++;

   }



   if(a<=2) cout<<"YES"<<endl;



   else if(a>2) cout<<"NO"<<endl;

}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
//      https://youtu.be/X_hMZYDMps4



#include<bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef long double ld;

typedef long long unsigned llu;

typedef pair<long long, long long> pll;

const long long inf = 2000000000000000000LL;    // 2e18

#define pi                  acos(-1.0)

#define fast                ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define nl                  "\n"

#define ff                  first

#define ss                  second

#define pb                  push_back

#define pf                  push_front

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

#define debug(x)            cout<<"debug "<<x<<"\n"



int main()

{

    fast;

    ll t,i,j,k,l,m,n,o,p,q,temp,ans=0,flag=0,mod=1000000007;

    cin>>n;

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

        cin>>temp;

        if(temp!=i) ans++;

    }

    if(ans<=2) cout<<"YES";

    else cout<<"NO";

}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    int n,a[100],b[100],c=0;

    scanf("%d",&n);

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

    {

        cin>>a[i];

    }

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

    {

        b[i]=a[i];

    }

    sort(a,a+n);

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

    {

        if(a[i]!=b[i])

        {

            c++;

        }

    }

    if(c==0 || c==2)

    {

        cout<<"YES"<<endl;

    }

    else

    {

        cout<<"NO"<<endl;

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <cstring>

#include <ctype.h>

#include<stdlib.h>



#define flin            freopen("input.txt", "r", stdin); 

#define flout           freopen("output.txt", "w", stdout);

#define ll              long long

#define ull             unsigned long long

#define fastio          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#define nl              '\n'

#define nll             '\0'

#define pb              push_back

#define bdyptb          return 0;

#define sorted(x)       sort(x.begin(), x.end())

#define reversed(x)     reverse(x.begin(), x.end())

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

#define mset(a, b)      memset(a, b, sizeof(a));

#define testcase        ll t,tt; cin>>tt; for(t=1;t<=tt;t++)



#define pi acos(-1.00)   

#define mx 1000007



using namespace std;



int main()

{

    fastio;

    int n;

    cin>>n;

    int a[n];

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

    {

        cin>>a[i];

    }

    int x=0; 

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

    {

        if(a[i]!=i+1)x++;

    }

    if(x==2 ||x==0)cout<<"YES\n";

    else cout<<"NO\n";

}

// Optimized solution to the same problem (faster):
#include <string.h>

#include <stdio.h>

#include <math.h>

int main ()

{

    int n, c=0;

    scanf ("%d", &n);

    int a[n];

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

        scanf ("%d", &a[i]);

        if (a[i]!=i+1) c++;

    }

    if (c>2) printf ("NO");

    else printf ("YES");

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

using namespace std;



int main()

{

    int n, i;



    cin>>n;



    vector<int>P(n);



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

    {

        cin>>P.at(i);

    }

    int j, k=0;



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

    {

        if(P.at(j)!=j+1)

            k++;

    }

    if (k<=2)

        cout<<"YES"<<endl;



    else

        cout<<"NO"<<endl;



        return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

    int i,j,a[60],b[60],n,flag;



    scanf("%d",&n);



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

    {

        scanf("%d",&a[i]);

    }



    for(i=0,j=1;i<n;i++,j++)

    {

        b[i] = j;

    }



    for(i=0,flag=0;i<n;i++)

    {

        if(a[i]!=b[i])

            flag++;

    }



    if(flag<=2)

        printf("YES");

    else

        printf("NO");



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

 

int main(){

  int N;

  cin>>N;

  vector<int> P(N);

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

    cin>>P.at(i);

  }

  int k=0;

  for(int j=0;j<N;j++){

    if(P.at(j)!=j+1) k++;

  }

  if (k<=2) cout<<"YES"<<endl;

  else cout<<"NO"<<endl;

 

}

// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

    int i,j,a[60],b[60],n,flag;



    scanf("%d",&n);



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

    {

        scanf("%d",&a[i]);

    }



    for(i=0,j=1;i<n;i++,j++)

    {

        b[i] = j;

    }



    for(i=0,flag=0;i<n;i++)

    {

        if(a[i]!=b[i])

            flag++;

    }



    if(flag<=2)

        printf("YES");

    else

        printf("NO");



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <bits/stdc++.h>

using namespace std;





int main() {

    int n;

    cin>>n;

    vector<int> p(n);

    int count=0;

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

    {

        cin>>p[i];

        if(p[i]!=i+1) 

        count++;

    }

    if(count==2 || count==0)

     cout<<"YES"<<endl;

    else

     cout<<"NO"<<endl;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

    int i,j,a[60],b[60],n,flag;



    scanf("%d",&n);



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

    {

        scanf("%d",&a[i]);

    }



    for(i=0,j=1;i<n;i++,j++)

    {

        b[i] = j;

    }



    for(i=0,flag=0;i<n;i++)

    {

        if(a[i]!=b[i])

            flag++;

    }



    if(flag<=2)

        printf("YES");

    else

        printf("NO");



    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <stdio.h>

#include <string.h>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <queue>

#include <math.h>

#include <assert.h>

#include <set>

#include <map>

#include <bitset>

#include <ctime>

#include <time.h>

#include <algorithm>

#include <cstdio>

#include <fstream>

#include <stack>

#include <ctype.h>

#include <numeric>

#include <sstream>

#include <unistd.h>

#include <unordered_map>

#include <limits>

#include <random>

#include <chrono>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



int main(int argv, char **argc){

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



    int n, a[50], b[50], cnt = 0;

    cin >> n;

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

        cin >> a[i];

        b[i] = a[i];

    }

    sort(a, a + n);

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

        cnt += (a[i] != b[i]);

    }

    printf("%s\n", cnt <= 2 ? "YES" : "NO");

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int main()

{

    int a,z=1,i,sum=0;

    scanf("%d",&a);

    int c[a];

    for(i=0;i<a;i++){

        scanf("%d",&c[i]);

    }

    for(i=0;i<a;i++,z++)

    {

       if(c[i]!=z) sum++;

    }

    if(sum<=2)printf("YES\n");

    else printf("NO\n");

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h> 

 

  using namespace std; 







int main()

{

     int n,i=0,c=0,num;

     scanf("%d",&n);

     vector<pair<int, int> >v,s;

     while(n>0)

     {

         scanf("%d",&num);

         v.push_back(make_pair(num,i));

         i++;

          n--;

     }

     s=v;

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

     for(i=0;i<v.size();i++)

     {

         if(v[i].first!=s[i].first)

         {

             c++;

         }

     }

     if(c==2 || c==0)

     {

         printf("YES");

     }

     else

     {

         printf("NO");

     }

     

     

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int main()

{

    int a,z=1,i,sum=0;

    scanf("%d",&a);

    int c[a];

    for(i=0;i<a;i++){

        scanf("%d",&c[i]);

    }

    for(i=0;i<a;i++,z++)

    {

       if(c[i]!=z) sum++;

    }

    if(sum<=2)printf("YES\n");

    else printf("NO\n");

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



void swapp(int a[],int i,int j);

bool sorted(int a[],int s);



int main()

{

    int n;

    bool x = false;

    cin>>n;

    int arr[n];

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

    {

        cin>>arr[i];

    }

    if(sorted(arr,n))

    {

        cout<<"YES";

    }

    else

    {

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

        {

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

            {

                if(arr[j]<arr[i])

                {

                    swapp(arr,i,j);

                    if(sorted(arr,n))

                    {

                        x = true;

                        break;

                    }

                    swapp(arr,j,i);

                }

            }

        }

        if(x)

        {

            cout<<"YES";

        }

        else

        {

            cout<<"NO";

        }

    }

}



void swapp(int a[],int i,int j)

{

    int temp = a[j];

    a[j] = a[i];

    a[i] = temp;

}



bool sorted(int a[],int s)

{

    bool x = true;

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

    {

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

        {

            x = false;

            break;

        }

    }

    return x;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

int main()

{

    int a,z=1,i,sum=0;

    scanf("%d",&a);

    int c[a];

    for(i=0;i<a;i++){

        scanf("%d",&c[i]);

    }

    for(i=0;i<a;i++,z++)

    {

       if(c[i]!=z) sum++;

    }

    if(sum<=2)printf("YES\n");

    else printf("NO\n");

    return 0;

}


// Fastest known solution for this problem: