Submission #1805199


Source Code Expand

#include <cstdio>
#include <iostream>
typedef long long ll;
inline int Get() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int Num = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9')
		Num = (Num << 3) + (Num << 1) + ch - '0';
	return Num;
}
const int N = 2e5 + 5;
const int inf = 2e9;
int n, a[N], lc[N], rc[N];
ll ans;
void solve(int l, int r) {
	if (l == r) {
		ans += a[l];
		return;
	}
	
	int mid = l + r >> 1;
	
	lc[mid + 1] = rc[mid] = inf;
	for (int i = mid; i >= l; --i) lc[i] = std :: min(lc[i + 1], a[i]);
	for (int i = mid + 1; i <= r; ++i) rc[i] = std :: min(rc[i - 1], a[i]);
	
	int tl = mid, tr = mid + 1;
	while (tl >= l || tr <= r) 
		if (tl < l || (tr <= r && rc[tr] > lc[tl])) ans += (ll)rc[tr++] * (mid - tl);
		else ans += (ll)lc[tl--] * (tr - mid - 1);
	
	solve(l, mid), solve(mid + 1, r);
	return;
}
int main() {
	n = Get();
	for (int i = 1; i <= n; ++i) a[i] = Get();
	
	solve(1, n);
	printf("%lld\n", ans);
}

Submission Info

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

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 1 ms 256 KB
corner3 AC 26 ms 2560 KB
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
maxrand0 AC 29 ms 2560 KB
maxrand1 AC 29 ms 2560 KB
maxrand2 AC 29 ms 2560 KB
rand0 AC 1 ms 256 KB
rand1 AC 1 ms 256 KB
rand2 AC 1 ms 256 KB