Problem description
This day is smallVBirthday, he received a gift from his friends.
Now, LittleVYesNPiece of gift, he would have thisNThe gift is lined up in a row, numbered1n a satisfactory value w[i] Now small v [l, R]happiness large.
[l, R] ([l, R]* ([l, R] All gift satisfaction values in the and)
small v want to know what he can get happiness Span class= "FONTSTYLE0" > What is the maximum, can you help him?
★ data input
first behavior a positive integer n Second behavior n integers w[1], w[2], ..., W[n]
Span class= "Fontstyle0", Where:
for 50%1<=n<=100, 0<=w[i]<=100
for 80%1<=n<=1,000, 0<=w[i] <=1,000
100%1<=n<=100,000, 0<=w[i]<=10,000
★ data output
The maximum happiness value that small V can obtain.
★
Input
3
1 2 3
Output
10
Input
3
2 1 3
Output
9
Thinking of solving problems
Violence
Each number is a starting point, expanding to either side, extending to a number smaller than it or the array boundary to stop. Expansion process Statistics sum
The number that is the starting point is the minimum value of this number.
Update the final answer with (sum* as the number of starting points) max_happiness
Code
1#include <stdio.h>2#include <stdlib.h>3 4 intMain ()5 {6 inti,j,k;7 intN;8scanf"%d",&n);9 int*p = (int*)malloc(sizeof(int)*n);Ten for(i=0; i<n;i++) Onescanf"%d", p+i); A //------------------------------------- -__int64 maxhap=0; - the for(i=0; i<n;i++) - { -__int64 sum=0; - for(j=i-1; j>=0&&p[j]>=p[i];j--) +sum+=P[j]; - for(k=i;k<n&&p[k]>=p[i];k++) +sum+=P[k]; AMaxhap = Sum*p[i]>maxhap? sum*P[i]: Maxhap; at } -printf"%i64d", Maxhap); - //------------------------------------- - Free(p); - return 0; -}
Data Structure Happiness