Poj2796feel good[monotonic Stack]

Source: Internet
Author: User
Tags integer numbers

Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13376 Accepted: 3719
Case Time Limit: 1000MS Special Judge

Description

Bill is developing a new mathematical theory for human emotions. His recent investigations is dedicated to studying what good or bad days influent people ' s memories about some period of l Ife.

A new Idea Bill had recently developed assigns a non-negative integer value to each of the human life.

Bill calls this value, the emotional value of the day. The greater the emotional value is, the better the Daywas. Bill suggests the value of some period of human life are proportional to the sum of the emotional values of n the given period, multiplied by the smallest emotional value of the th in it. This schema reflects, good on average period can is greatly spoiled by one very bad day.

Now Bill was planning to investigate he own life and find the period of the had value. Help him.

Input

The first line of the input contains n-the number of days of Bill's life he's planning to investigate (1 <= n <= 1 00 000). The rest of the file contains n integer numbers a1, a2, ... a ranging from 0 to 106-the emotional values of the days. Numbers is separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill ' s life on the first line. And on the second, line print, numbers l and r such, the period from l-th to R-th day of Bill's life (inclusive) have The greatest possible value. If there is multiple periods with the greatest possible value,then print any one of the them.

Sample Input

63 1 6 4 5 2

Sample Output

603 5

Source

Northeastern Europe 2005 Test instructions: nonnegative integers, one interval, interval and * interval min maximum The idea is to find the longest interval (i.e. the largest) with each point as the minimum value .the method to find is the monotonous stackmaintains a descending stack of values, h values, L is the and of the interval, and B is the starting position (that is, where the left extends) for the number of I, go to the left, find the bigger one and use it to update the answer and pop it (because it can no longer extend to the right, i-1)and Insert this number, and the position of the insertion is the leftmost position of the number.This process maintains a right, which means extending theThe last stack is the right-to-N.
////main.cpp//poj2796////Created by Candy on 10/5/16.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong Longll;Const intn=1e5+5; inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx;}intn;ll A,ans=-1, Ansb,anse;structdata{ll H,l; intb;} St[n];inttop=0;intMainintargcConst Char*argv[]) {N=read ();  for(intI=1; i<=n;i++) {a=read (); ll Right=0;intb=i;  while(top&&st[top].h>=a) {LL tmp= (st[top].h* (st[top].l+Right )); Right+=ST[TOP].L; b=st[top].b; if(tmp>ans) {ans=tmp; ANSB=st[top].b; Anse=i-1;//printf ("T%lld%lld%lld\n", ans,ansb,anse);} top--; } Top++; St[top].h=A; ST[TOP].L=right+A; ST[TOP].B=b; } ll Right=0;  while(top) {ll tmp= (st[top].h* (st[top].l+Right )); Right+=ST[TOP].L; if(tmp>ans) {ans=tmp; ANSB=st[top].b; Anse=O; } Top--; } printf ("%lld\n%lld%lld", Ans,ansb,anse); return 0;}

Poj2796feel good[monotonic Stack]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.