就是顺着环走,模拟一下,不能回头,所以遇到小的门牌号,就只能转一圈转回来
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = int (1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
int main() {
LL n,m,i,j,ans,a,b;
while (cin>>n>>m) {
ans=0; b=1;
for (int i=0; i<m; i++) {
cin>>a;
if (a>=b) { ans=ans+a-b; }
else { ans=ans+a+n-b; }
b=a;
}
cout<<ans<<endl;
}
return 0;
}