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. maven 配置(cmd 黑窗口执行 mvn 时默认的 settings 文件和 idea maven 相关配置)

    写在前面: 本文章用于记录博主平时遇到的问题,步骤略粗糙,目的在于记录一边后续博主自己查找,如果能帮助到其他人更好.文章中用到的链接均为自行引入,侵删,谢谢(2I2Rc*@JY8) 问题说明:在一次使 ...

  2. JTAG串链

  3. matlab 2018b 下载链接

    matlab 2018b 功能强大下载地址为 https://pan.baidu.com/s/1QZO35BtzcIkh_yPYRIGVWg

  4. 数字孪生与GIS的结合:创新灾害预防管理的未来

    近年来,全球频发的自然灾害给人们的生命和财产安全带来了巨大威胁,灾害预防管理成为当务之急.然而,随着数字孪生技术和GIS的迅猛发展,一种全新的解决方案正在崭露头角.数字孪生与GIS的结合,为灾害预防管 ...

  5. NLP复习之N元文法

    N元文法的统计 二元概率方程: \[P(w_n|w_{n-1}) = \frac{C(w_{n-1}w_n)}{C(w_{n-1})} \] 三元概率估计方程: \[P(w_n|w_{n-2},w_{ ...

  6. 从零玩转设计模式之简单工厂设计模式-jiandangonchangmoshi

    title: 从零玩转设计模式之简单工厂设计模式 date: 2022-12-08 11:31:19.472 updated: 2022-12-11 23:03:34.805 url: https:/ ...

  7. 【Python】人工智能-机器学习——不调库手撕贝叶斯分类问题

    1. 作业内容描述 1.1 背景 数据集大小150 该数据有4个属性,分别如下 Sepal.Length:花萼长度(cm) Sepal.Width:花萼宽度单位(cm) Petal.Length:花瓣 ...

  8. Mybatis源码5 StatementHandler ,ParameterHandler

    Mybatis5 StatementHandler ,ParameterHandler 一丶概述 前面我们总结了SqlSession--->CachingExecutor--->BaseE ...

  9. flutter中全局与单页面背景图片(动态图片)

    单页面设置背景图片 使用 Container 组件和 decoration 属性: 优点:简单易用,适用于大多数情况下的页面背景设置. 缺点:无法控制背景图片的位置和层级. class MyBook ...

  10. C++篇:第四章_数组_知识点大全

    C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 四.数组 (一)数组标准 以数组名作函数参数时,实参数组与形参数组都不必定义长度 ...