A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.

By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.

For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.

Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.

Input

The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a 1, a 2, ..., a n (1 ≤ a i ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a 1, a 2, ..., a n are not necessarily different.

Output

Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.

Examples

input

4
33 44 11 22

output

2

input

7
10 10 58 31 63 40 76

output

Copy

10

Note

In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).

In the second sample the colonel may swap the soldiers in the following sequence:

  1. (10, 10, 58, 31, 63, 40, 76)
  2. (10, 58, 10, 31, 63, 40, 76)
  3. (10, 58, 10, 31, 63, 76, 40)
  4. (10, 58, 10, 31, 76, 63, 40)
  5. (10, 58, 31, 10, 76, 63, 40)
  6. (10, 58, 31, 76, 10, 63, 40)
  7. (10, 58, 31, 76, 63, 10, 40)
  8. (10, 58, 76, 31, 63, 10, 40)
  9. (10, 76, 58, 31, 63, 10, 40)
  10. (76, 10, 58, 31, 63, 10, 40)
  11. (76, 10, 58, 31, 63, 40, 10)

题意

为数据排序,移动最大值到最前方,最小值到最后一位

输入的时候不断比较,但在出现等于最小值时更新位置(让定位更接近最后一位)

当最大值在最小值后时,总移动一次数 - 1

#include<bits/stdc++.h>
using namespace std;
int n, i, p, q = 99, x, a, b;
int main() {
for (cin >> n; i++ < n;)
cin >> x, x > p ? p = x, b = i : 0, x <= q ? q = x, a = i : 0;
cout << b + n - a - 1 - (b > a);//判断最大值是否在最小值后
}

Codeforces 144A Arrival of the General (水)的更多相关文章

  1. codeforces Arrival of the General 题解

    A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...

  2. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  3. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  4. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  5. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  6. C - Arrival of the General

    Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...

  7. codeforces 712B B. Memory and Trident(水题)

    题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. codeforces 712A A. Memory and Crow(水题)

    题目链接: A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. codeforces 711A A. Bus to Udayland(水题)

    题目链接: A. Bus to Udayland 题意: 找一对空位坐下来,水; 思路: AC代码: #include <iostream> #include <cstdio> ...

  10. Codeforces Round #365 (Div. 2) A 水

    A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. 火山引擎ByteHouse:如何优化ClickHouse物化视图能力?

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 近期,火山引擎 ByteHouse 升级了基于 ClickHouse 的物化视图能力,为解决数据量爆炸式增长带来的 ...

  2. python计算代码运行时间

    记录一下自己用python编写计算运行时间的代码 时间类 import time import numpy as np # 编写时间类来方便操作 class Timer: def __init__(s ...

  3. NodeJS连接mysql,报错ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

    我是mysql8.0以上的版本,在用NodeJS连接服务器中mysql数据库时开始报错 这表示服务器启动起来,但是数据库中密码协议出错,我从网上查到的结果告诉我,是mysql8.0支持了一个新的密码协 ...

  4. 2023-12-13:用go语言,密码是一串长度为n的小写字母,一则关于密码的线索纸条, 首先将字母a到z编号为0到25编号, 纸条上共有n个整数ai,其中a1表示密码里第一个字母的编号, 若i>1的

    2023-12-13:用go语言,密码是一串长度为n的小写字母,一则关于密码的线索纸条, 首先将字母a到z编号为0到25编号, 纸条上共有n个整数ai,其中a1表示密码里第一个字母的编号, 若i> ...

  5. SpringBoot事件机制

    1.是什么? SpringBoot事件机制是指SpringBoot中的开发人员可以通过编写自定义事件来对应用程序进行事件处理.我们可以创建自己的事件类,并在应用程序中注册这些事件,当事件被触发时,可以 ...

  6. NC65元数据添加七彩版时注意点

    元数据添加七彩版时注意点 元数据七彩版模式 --- 主要添加Xml文件调整格式 添加时注意点如下 手动创建Panel时 自动生成的实现方法中有一个方法的返回值一定要为true 该方法主要是控制显不显示 ...

  7. Spring源码学习之Web数据绑定器WebDataBinder

    WebDataBinder 1.描述 特殊的数据绑定器用于从web请求参数到JavaBean对象的数据绑定.专为web环境,但不依赖于Servlet API;作为更具体的DataBinder变体的基类 ...

  8. 从零玩转设计模式之建造者模式-jianzaozhemoshi

    title: 从零玩转设计模式之建造者模式 date: 2022-12-08 18:15:30.898 updated: 2022-12-23 15:35:58.428 url: https://ww ...

  9. 从零玩转Yaip使用-cong-ling-wan-zhuan-yaip-shi-yong

    title: 从零玩转Yaip使用 date: 2021-07-16 15:47:17.624 updated: 2021-12-26 17:43:12.255 url: https://www.yb ...

  10. 【OpenVINO】 使用 OpenVINO CSharp API 部署 PaddleOCR 项目介绍

    前言:   在之前的项目中,我们已经使用 OpenVINOTM CSharp API 部署 PaddleOCR 全系列模型,但随着PaddleOCRv4版本发布以及OpenVINO CSharp AP ...