Sphere Online Judge

Computer geeks! Show off your skills and claim the job of your dreams! RecruitCoders.com

SPOJ Problem Set (classical)

3436. Histogram

Problem code: HIST2

In statistics, a histogram is a graphical display of tabulated frequencies, shown as bars. It shows what proportion of cases fall into each of several categories. It is a polygon composed of a sequence of rectangles aligned at a common base line. In this problem all rectangles have a width of unit length. But their heights are distinct. Some permutation of the heights will give the maximum perimeter. Your task is to find the maximum perimeter of the histogram and the number of permutations that give the maximum perimeter.

In the image Figure (a) shows a histogram with heights {1,2,3,4} (1st sample testcase) and has a perimeter of 16 units. Figure (b) shows one of the permutations {3,1,2,4} having the maximum perimeter of 20 units.

Input

Input consists of multiple test cases. Each test case describes a histogram and starts with an integer N, 2 ≤ N ≤ 15, denoting the number of rectangles it is composed of. Next line consists of N space separated positive integers representing the heights of the rectangles. All heights are distinct and less than or equal to 100. N=0 indicates the end of tests. There are atmost 50 test cases.

Output

For each test case output the maximum possible perimeter of the histogram and the number of permutations that give maximum perimeter in a single line, separated by a single space.

Example

Input:
4
1 2 3 4
3
2 6 5
0

Output:
20 8
24 2


Added by:Swarnaprakash
Date:2008-11-29
Time limit:4s
Source limit:50000B
Languages:All except: ERL JS PERL 6
Resource:Kurukshetra 09 OPC

hide comments
2011-10-31 16:16:51 prathamesh sonpatki
cant see the image ?
2011-10-31 16:14:57 prathamesh sonpatki


Last edit: 2011-10-31 16:17:40
2011-01-02 09:19:22 anurag
try observing a pattern how u would arrange to maximise the the difference of heights also dont forget to add 2*number of rectangles to this it cause me a few submissions
2010-01-03 02:46:25 SmitherS
@madhav Short answer: yes. The long answer would probably be a spoiler, but I can say that my solution turned out to be surprisingly straightforward, at least in code - arriving at it and convincing myself that it worked was more complicated. My background is maths not computer science and getting to my solution is a little involved; most programmers may well be more at home with your solution.

Last edit: 2010-10-27 00:37:01
2009-07-18 23:46:01 madhav
I solved the problem using dp with bitmasks . Is there any other method?
SPOJ System © 2012 Sphere Research Labs | Projekty informatyczne i aplikacje na zamówienie. All Rights Reserved.