Codeforces 381A Sereja and Dima

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/polanwind/article/details/87484863
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <math.h>
#include <string>
#include <algorithm>

using namespace std;

int n;
int temp[1005];
int a, b;
int l, r;

int main() {
	scanf("%d", &n);
	for (int i = 1;i <= n;++i) {
		scanf("%d", &temp[i]);
	}
	l = 1;
	r = n;
	for (int i = 1;i <= n;++i) {
		if (i % 2 == 1) {
			if (temp[l] > temp[r]) {
				a += temp[l];
				l++;
			}
			else {
				a += temp[r];
				r--;
			}
		}
		else {
			if (temp[l] > temp[r]) {
				b += temp[l];
				l++;
			}
			else {
				b += temp[r];
				r--;
			}
		}
	}
	printf("%d %d\n", a, b);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/polanwind/article/details/87484863