求某段區間的最小值*這段區間全部元素之和的最大值;ios
#include<iostream> #include<cstdio> using namespace std; const int N = 100010; typedef long long ll; ll a[N], st[N], sum[N]; int n, top = 0; ll ans = -1, l, r; int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; //前綴和預處理 a[n + 1] = -1; for(int i = 1; i <= n + 1; i++){ while(top && a[st[top]] > a[i]){ ll temp = (ll)a[st[top]] * (sum[i - 1] - sum[st[top - 1]]); if(ans < temp){ ans = temp; l = st[top - 1] + 1; r = i - 1; } --top; } ++top; st[top] = i; } printf("%lld\n%lld %lld\n", ans, l, r); return 0; }