結果

問題 No.1374 Absolute Game
ユーザー kotatsugame
提出日時 2021-02-05 21:40:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 637 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 71,300 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-02 11:53:22
合計ジャッジ時間 2,105 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    6 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
int N;
int A[1<<17];
main()
{
	cin>>N;
	for(int i=0;i<N;i++)cin>>A[i];
	sort(A,A+N);
	long L,R;
	{
		long X=0,Y=0;
		for(int i=0;i<N;i++)
		{
			if(i%2==0)X+=A[i];
			else Y+=A[i];
		}
		L=abs(X)-abs(Y);
		X=Y=0;
		for(int i=0;i<(N+1)/2;i++)X+=A[i];
		for(int i=(N+1)/2;i<N;i++)Y+=A[i];
		L=min(L,abs(X)-abs(Y));
	}
	{
		long X=0,Y=0;
		for(int i=0;i<N;i++)
		{
			if(i%2==0)X+=A[N-i-1];
			else Y+=A[N-i-1];
		}
		R=abs(X)-abs(Y);
		X=Y=0;
		for(int i=0;i<(N+1)/2;i++)X+=A[N-i-1];
		for(int i=(N+1)/2;i<N;i++)Y+=A[N-i-1];
		R=min(R,abs(X)-abs(Y));
	}
	cout<<max(L,R)<<endl;
}
0