結果

問題 No.3289 Make More Happy Connection
ユーザー pengin_2000
提出日時 2025-10-03 22:04:58
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 1,273 ms
コンパイル使用メモリ 27,180 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2025-10-03 22:05:02
合計ジャッジ時間 3,073 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:7:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 |         scanf("%lld", &n);
      |         ^~~~~~~~~~~~~~~~~
main.c:10:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |                 scanf("%lld %lld", &x[i], &y[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
long long int x[300005], y[300005];
long long int dp[300005][2];
int main()
{
	long long int n;
	scanf("%lld", &n);
	long long int i;
	for (i = 0; i < n; i++)
		scanf("%lld %lld", &x[i], &y[i]);
	if (x[0] == y[0])
		dp[0][0] = dp[0][1] = x[0];
	else
		dp[0][0] = dp[0][1] = 0;
	long long int res1, res2;
	for (i = 1; i < n; i++)
	{
		res1 = dp[i - 1][0];
		if (y[i - 1] == x[i])
			res1 += x[i];
		res2 = dp[i - 1][1];
		if (x[i - 1] == x[i])
			res2 += x[i];
		if (res1 > res2)
			dp[i][0] = res1;
		else
			dp[i][0] = res2;
		res1 = dp[i - 1][0];
		if (y[i - 1] == y[i])
			res1 += y[i];
		res2 = dp[i - 1][1];
		if (x[i - 1] == y[i])
			res2 += y[i];
		if (res1 > res2)
			dp[i][1] = res1;
		else
			dp[i][1] = res2;
		if (x[i] == y[i])
		{
			dp[i][0] += x[i];
			dp[i][1] += x[i];
		}
	}
	res1 = dp[n - 1][0];
	if (res1 < dp[n - 1][1])
		res1 = dp[n - 1][1];
	printf("%lld\n", res1);
	return 0;
}
0