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 m things to do. In order to complete the i-th task, she needs to be in the house number a i 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 a 1, a 2, ..., a m (1 ≤ a i ≤ 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.

思路:

路径移动类似循环队列移动,注意点:使用long long 保证不溢出

#include<bits/stdc++.h>
using namespace std;
long long m, n, x = 1, s, y;
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
for (cin >> n >> m; m--; s += (n + y - x) % n, x = y)//x起点位置,y目标位置
cin >> y;
cout << s;
}

CodeForces-339B-Xenia and Ringroad (循环队列,水题)的更多相关文章

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

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

  2. codeforces Gym 100187L L. Ministry of Truth 水题

    L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...

  3. Codeforces Round #185 (Div. 2) B. Archer 水题

    B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...

  4. Educational Codeforces Round 14 A. Fashion in Berland 水题

    A. Fashion in Berland 题目连接: http://www.codeforces.com/contest/691/problem/A Description According to ...

  5. Codeforces Round #360 (Div. 2) A. Opponents 水题

    A. Opponents 题目连接: http://www.codeforces.com/contest/688/problem/A Description Arya has n opponents ...

  6. Codeforces Round #190 (Div. 2) 水果俩水题

    后天考试,今天做题,我真佩服自己... 这次又只A俩水题... orz各路神犇... 话说这次模拟题挺多... 半个多小时把前面俩水题做完,然后卡C,和往常一样,题目看懂做不出来... A: 算是模拟 ...

  7. Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告

    对于这道水题本人觉得应该应用贪心算法来解这道题: 下面就贴出本人的代码吧: #include<cstdio> #include<iostream> using namespac ...

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

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

  9. Codeforces 777D:Cloud of Hashtags(水题)

    http://codeforces.com/problemset/problem/777/D 题意:给出n道字符串,删除最少的字符使得s[i] <= s[i+1]. 思路:感觉比C水好多啊,大概 ...

  10. Codeforces Round #340 (Div. 2) B. Chocolate 水题

    B. Chocolate 题目连接: http://www.codeforces.com/contest/617/problem/D Descriptionww.co Bob loves everyt ...

随机推荐

  1. tortoiseGit教程(常用图文教程)

    需求: gitTorise是git的比较好用的一个图形化工具,本文目的在于对tortoiseGit常见使用进行一个总结. 对于git常见的使用有: 1. 建立仓库 2. 提交代码 3. 更新代码 4. ...

  2. DDD学习与感悟——总是觉得自己在CRUD怎么办?

    一.DDD是什么? DDD全名叫做Domins drives Design:领域驱动设计.再说的通俗一点就是:通过领域建模的方式来实现软件设计. 问题来了:什么是软件设计?为什么要进行软件设计? 软件 ...

  3. [ARC145B] AB Game

    The game is played by Alice and Bob. Initially, there are $n$ stones. The players alternate turns, m ...

  4. shell 脚本中的 '-f' 和 '-d' 分别代表什么意思

    shell脚本中,'-f' 和 '-d'是用于测试文件类型的条件表达式. 1.'-f'表达式: 表达式: '[ -f file ]' 描述: 判断给定路径是否是一个常规文件 (regular file ...

  5. Spring Boot内置的一些工具类

    1.断言Assert工具类 // 要求参数 object 必须为非空(Not Null),否则抛出异常,不予放行 // 参数 message 参数用于定制异常信息. void notNull(Obje ...

  6. Critical error detected c0000374

    我发现出现上述错误是 free 两次内存 float* dd=new float[2]; delete[] dd; delete[] dd;

  7. 【2016】开机出现 system32\config\system,代码:0xc00000e9解决方法

    这是16年刚工作时写的笔记,也带来这里做个记录吧.实际工作这几年里也时不时会遇到,大多数和非正常关机有关系 今天早上,就在刚才,一个同事的电脑开不了机了,开机提示的是system32\config\s ...

  8. 万界星空科技QMS质量管理系统

    QMS(Quality Management System)质量管理系统是五大基础系统之一,在工业企业中被广泛的应用,在质量策划.生产过程质量监督.体系审核和文档管理等业务上发挥着不可替代的作用. 一 ...

  9. MinIO客户端之mv

    MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc mv 将对象在桶之间移动. 在桶内准备好待移动的文件,检查对象,命令如下: ./mc ls local1/b ...

  10. ElasticSearch之cat segments API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/segments?v=true&pretty" --cacert $ES_ ...