Skip to main content
  1. Posts/

codeforces 442C. Artem and Array

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

C. Artem and Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn’t have an adjacent number to the left or right, Artem doesn’t get any points.

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

Input

The first line contains a single integer n (1 ≤ n ≤ 5*105) – the number of elements in the array. The next line contains n integers a__i(1 ≤ a__i ≤ 106) – the values of the array elements.

Output

In a single line print a single integer – the maximum number of points Artem can get.

Sample test(s)

input

5

3 1 5 2 6

output

11

input

5

1 2 3 4 5

output

6

input

5

1 100 101 100 1

output

102

并不会做.

接触了一个交单调栈的东西…

又是单调栈又是单调队列

是时候仔细了解下了.

 1
 2
 3    /*************************************************************************
 4    	> File Name: code/2015summer/#3/D.cpp
 5    	> Author: 111qqz
 6    	> Email: rkz2013@126.com
 7    	> Created Time: 2015年07月28日 星期二 13时47分48秒
 8     ************************************************************************/
 9
10    #include<iostream>
11    #include<iomanip>
12    #include<cstdio>
13    #include<algorithm>
14    #include<cmath>
15    #include<cstring>
16    #include<string>
17    #include<map>
18    #include<set>
19    #include<queue>
20    #include<vector>
21    #include<stack>
22    #define y0 abc111qqz
23    #define y1 hust111qqz
24    #define yn hez111qqz
25    #define j1 cute111qqz
26    #define tm crazy111qqz
27    #define lr dying111qqz
28    using namespace std;
29    #define REP(i, n) for (int i=0;i<int(n);++i)
30    typedef long long LL;
31    typedef unsigned long long ULL;
32    const int N=5E5+7;
33    int a[N],b[N];
34    int n;
35    int main()
36    {
37        cin>>n;
38        int x;
39        int top = -1;
40        LL ans = 0;
41        for ( int i = 1 ; i <= n ; i++ )
42        {
43    	  scanf("%d",&x);
44    	  while (top>=1&&a[top-1]>=a[top]&&a[top]<=x)
45    	  {
46    	      ans = ans + min(x,a[top-1]);
47    	      top--;
48    	  }
49    	  top++;
50    	  a[top]=x;
51        }
52
53
54        sort(a,a+top+1);
55        for ( int i = 0 ; i <top-1 ; i++)
56        {
57    	ans = ans + a[i];
58        }
59        cout<<ans<<endl;
60
61
62    	return 0;
63    }

Related

cf 442B Andrey and Problem

·2 mins
B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends – the probability that this friend will come up with a problem if Andrey asks him.

cf 443B Kolya and Tandem Repeat

·1 min
B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.

(BC 一周年) hdu 5312 Sequence

·2 mins
比赛的时候没做出来.这道题需要用到的一个重要的性质是,任意一个自然数可以表示成至多三个三角形数(1,3,6,10,15…..)的和(orz高斯)然后也有推广到任意自然数可以表示成k个k角形数的和的结论(费马提出了猜想,柯西给了证明)然后官方题解说的比较好:

三角形数_百度百科

·2 mins
它有一定的规律性,排列如下(构成图),像上面的1、3、6、10、15等等这些能够表示成三角形的形状的总数量的数,叫做三角形数。