链家网2

输入

5

1 3 1 5 2

输出

43

#include <iostream>
#include <malloc.h>
#include <stdio.h>
using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	int p[10] = {0};
	int a[10] = {0};
	int i = 0;
	for(i=0;i<n;i++)
	{
		scanf("%d",&p[i]);
	}

	i = 0;
	int j = n-1;
	int count = 0;
	for(int k=0;k<n;k++)
	{
		if(p[i]<p[j])
		{
			a[k] = p[i];
			i++;
		}
		else if(p[i]>p[j])
		{
			a[k] = p[j];
			j--;
		}
	}
	
	for(i=0;i<n;i++)
	{
		count += a[i]*(i+1);
	}

	printf("%d",count);
	getchar();
	return 0;
}
发布了163 篇原创文章 · 获赞 183 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_31339221/article/details/77814932