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. Ansible 利用playbook批量部署mariadb

    环境说一下 192.168.30.21     ansible 192.168.30.25     client1 192.168.30.26     client2 这里我的ansible环境已经部 ...

  2. tomcat 编码设置

    在Tomcat8.0之前的版本,如果你要向服务器提交中文是需要转码的(如果你没有修改server.xml中的默认编码),因为8.0之前Tomcat的默认编码为ISO8859-1. POST方式提交 r ...

  3. Async/await语法糖实现(Generator)

    // generator也是一种迭代器(Iterator) 有next方法,并返回一个对象{value:...,done:...} function run(generatorFunction) { ...

  4. 字符串拼接原理 javac 和 javap

    一.新建一个 Test1 类 public class Test1 { public static void main(String[] args) { String a = "a" ...

  5. nyoj_8_一种排序_201311251238

    一种排序 时间限制:3000 ms  |           内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编 ...

  6. Spring MVC 入门(一)

    什么是 Spring MVC 学习某一样东西之前,我们一定要大致知道这个东西是什么,能干什么,为什么要用它. Spring MVC 是一个开源平台,一个基于 Spring 的 MVC 框架,它支持基于 ...

  7. firedac数据集和字符串相互转换

    uses Data.FireDACJSONReflect 1)FIREDAC数据库序列为字符串,进行了加压和BASE64编码 function DataSetToString(const ADataS ...

  8. Gulp帮你自己主动搞定coffee和scss的compile

    今天继续说说gulp的watch,能够自己主动搞定非常多事情.不用每次都去敲命令了! 上次说到用gulp能够非常方便的进行css,js,html的压缩.而且能够对coffee和scss进行编译. cs ...

  9. poj 2931 Building a Space Station &lt;克鲁斯卡尔&gt;

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2 ...

  10. Android中检測字符编码(GB2312,ASCII,UTF8,UNICODE,TOTAL——ENCODINGS)方法(一)

    package com.android.filebrowser;   import java.io.*; import java.net.*;   public class FileEncodingD ...