B. Xenia and Ringroad Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t=0,p;
    long long int n,m,a[100000],i;
    scanf("%lld%lld",&n,&m);
    scanf("%lld",&a[0]);
    t= (a[0]-1);
    p=a[0];

    for (i=1;i<m;i++){
        scanf("%lld",&a[i]);
        if (a[i] > p){
            t+=a[i]-p;
        }
        else if (a[i] < p){
            t += (n-p)+(a[i]);
        }
        p=a[i];
    }
    printf("%lld\n",t);
    return 0;
}


No comments

Theme images by enot-poloskun. Powered by Blogger.