Problem description

Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.

Xenia has recently moved into the ringroad house number 1. As a result, she's got mthings to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.

Output

Print a single integer — the time Xenia needs to complete all tasks.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

4 3
3 2 3

Output

6

Input

4 3
2 3 3

Output

2

Note

In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.

解题思路:如果后一个数b比前一个数a大,就累加它们的差值b-a;如果后一个数b比前一个数a小,就先循环一圈到后一个数b,此时累加数为n-a+b,注意:因为和最大值可能为1e10(11位)已经爆int,所以ans要用long long。水过!

AC代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m;LL ans,a,b;
int main(){
cin>>n>>m;ans=,a=;
for(int i=;i<=m;++i){
cin>>b;
if(b>=a)ans+=b-a;
else ans+=n-a+b;
a=b;
}
cout<<ans<<endl;
return ;
}

C - Xenia and Ringroad的更多相关文章

  1. Codeforces 339B:Xenia and Ringroad(水题)

    time limit per test : 2 seconds memory limit per test : 256 megabytes input : standard input output ...

  2. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  3. CodeForces 339B Xenia and Ringroad(水题模拟)

    题意:给定 n 个地方,然后再给 m 个任务,每个任务必须在规定的地方完成,并且必须按顺序完成,问你最少时间. 析:没什么可说的,就是模拟,记录当前的位置,然后去找和下一个位置相差多长时间,然后更新当 ...

  4. codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题

    A.题意就是把字符串里面的数字按增序排列,直接上代码. #include <string.h> #include <stdio.h> #include <algorith ...

  5. Codeforces Round #197 (Div. 2) (A、B、C、D、E五题合集)

    A. Helpful Maths 题目大意 给一个连加计算式,只包含数字 1.2.3,要求重新排序,使得连加的数字从小到大 做法分析 把所有的数字记录下来,从小到大排序输出即可 参考代码 #inclu ...

  6. CodeForces Round 197 Div2

    这次出的题水爆了,借着这个机会终于把CF的号变蓝了.A. Helpful Mathstime limit per test2 secondsmemory limit per test256 megab ...

  7. Codeforces Round #197 (Div. 2)

    A.Helpful Maths 分析:将读入的字符转化为数字,直接排个序就可以了. #include <cstdlib> #include <cstring> #include ...

  8. codeforces 342E :Xenia and Tree

    Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...

  9. [codeforces 339]D. Xenia and Bit Operations

    [codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...

随机推荐

  1. Git学习总结三(工作区和暂存区、撤销修改)

    工作区和暂存区 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库(Repository) 工作区有一个隐藏目录.git, ...

  2. Unity中确定时间是否在一定范围内

    NowTime = DateTime.Now.ToLocalTime(); Timeyear = DateTime.Now.ToLocalTime().ToString("yyyy-MM-d ...

  3. Python 递归、匿名函数、map和filter day4

    一.递归---函数自己调用自己 1.一个错误递归的例子: count=0 def hello(): global count count+=1 print("count %s"%c ...

  4. VMware Workstation 15 安装教程

    注:操作系统必须是64位    软件:360软件管家获取 1.运行下载完成的Vmware Workstation虚拟机软件包. 虚拟机软件的安装向导初始界面 2.在虚拟机软件的安装向导界面单击“下一步 ...

  5. Vue 中Bus使用

    使用:兄弟组件之间传值: 安装: npm install vue-bus 在main.js 中引入vuebus: import Vue from 'vue'; import VueBus from ' ...

  6. ansible special topics

    1.加速模式运行playbook accelerate 对于使用ansible 1.5 及之后版本的用户,加速模式只在以下情况下有用处: (A) 管理红帽企业版 Linux 6 或者更早的那些依然使用 ...

  7. hdu_1009_FatMouse' Trade_201310280910

    FatMouse' Trade http://acm.hdu.edu.cn/showproblem.php?pid=1009 Time Limit: 2000/1000 MS (Java/Others ...

  8. 【ACM】hdu_zs3_1006_AB_201308101123

    A/B Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submission ...

  9. Clojure:读取xml

    在Clojure中读取XML是很容易的.比如我们有一个名叫strings.xml文件: <?xml version="1.0" encoding="utf-8&qu ...

  10. HDU 4534

    AC自动机+状态DP. 虽然很明显的AC自动机+状态DP题,但要分析问题上还是欠缺一点了.一直在犹豫枚举每一个字符选或不选的状态会不会超时,以为会达到状态有2^n,但其实根本没有.因为有很多状态是可以 ...