蒟蒻求条 玄关
查看原帖
蒟蒻求条 玄关
733958
lzc120518楼主2024/11/26 17:57
#include<bits/stdc++.h>
#define int long long
using namespace std;

int a[100005], f[100005], g[200005];
int st[25][100005], sum[100005];
stack<int> s, t;

int ask(int l, int r)
{
	int k = log2(r - l + 1);
	return min(st[k][l], st[k][r - (1<<k) + 1]);
}

signed main()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= n; i++)
    	sum[i] = sum[i - 1] + a[i];
    for(int i = 1; i <= n; i++)
    	f[i] = n + 1;
    for(int i = n; i >= 1; i--)
    {
        while(!s.empty() && a[i] < a[s.top()]) s.pop();
        if(!s.empty()) f[i] = s.top() - 1;
        s.push(i);
    }
    for(int i = 1; i <= n; i++)
    {
    	while(!t.empty() && a[i] < a[t.top()]) t.pop();
        if(!t.empty()) g[i] = t.top() + 1;
        t.push(i);
	}
	for(int i = 1; i <= n; i++)
		st[0][i] = a[i];
	for(int i = 1; i <= 20; i++)
		for(int j = 1; j + (1<<i) <= n; j++)
			st[i][j] = min(st[i - 1][j], st[i - 1][j + (1<<i - 1)]);
	int ans = 0;
    for(int i = 1; i <= n; i++)
    	ans = max(ans, ask(g[i], f[i]) * (sum[f[i]] - sum[g[i] - 1]));
    cout << ans << endl;
    return 0;
}
2024/11/26 17:57
加载中...