Submission #904469


Source Code Expand

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }


template<typename InpIt, typename OutIt, typename Val, typename Cmp>
void stackAlgorithm(InpIt beg, InpIt end, OutIt out, Cmp cmp = Cmp()) {
	int n = (int)std::distance(beg, end);
	for(int i = 0; i < n; ++ i) {
		int k = i;
		while(k > 0 && cmp(beg[k - 1], beg[i]))
			k = out[k - 1];
		out[i] = k;
	}
}
template<typename InpIt, typename OutIt, typename Cmp>
void stackAlgorithm(InpIt it, InpIt end, OutIt out, Cmp cmp = Cmp()) {
	return stackAlgorithm<InpIt, OutIt, typename iterator_traits<InpIt>::value_type, Cmp>(it, end, out, cmp);
}

int main() {
	int N;
	while(~scanf("%d", &N)) {
		vector<int> a(N);
		for(int i = 0; i < N; ++ i)
			scanf("%d", &a[i]);
		vector<int> L(N), R(N);
		stackAlgorithm(a.begin(), a.end(), L.begin(), greater_equal<int>());
		stackAlgorithm(a.rbegin(), a.rend(), R.rbegin(), greater_equal<int>());
		rep(i, N) R[i] = N - 1 - R[i];
		ll ans = 0;
		rep(i, N)
			ans += (ll)a[i] * (R[i] + 1 - i) * (i + 1 - L[i]);
		printf("%lld\n", ans);
	}
	return 0;
}

Submission Info

Submission Time
Task B - Minimum Sum
User anta
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1612 Byte
Status AC
Exec Time 30 ms
Memory 2560 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:32:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &a[i]);
                      ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample example0, example1, example2
All corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name Status Exec Time Memory
corner0 AC 26 ms 2560 KB
corner1 AC 25 ms 2560 KB
corner2 AC 2 ms 256 KB
corner3 AC 28 ms 2560 KB
example0 AC 2 ms 384 KB
example1 AC 2 ms 256 KB
example2 AC 2 ms 256 KB
maxrand0 AC 29 ms 2560 KB
maxrand1 AC 30 ms 2560 KB
maxrand2 AC 30 ms 2560 KB
rand0 AC 2 ms 256 KB
rand1 AC 2 ms 256 KB
rand2 AC 2 ms 256 KB