結果

問題 No.818 Dinner time
ユーザー leaf_1415
提出日時 2019-04-19 22:07:53
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 564 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 55,028 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-11-29 23:27:25
合計ジャッジ時間 2,744 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#define llint long long

using namespace std;

llint n, m;
llint a[100005], b[100005];
llint s[100005], mn[100005];

int main(void)
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
	
	for(int i = n; i >= 1; i--) s[i] = s[i+1] + max(a[i], b[i]);
	for(int i = n; i >= 1; i--) mn[i] = min(s[i], mn[i+1]);
	
	llint ans = -1e18, sum = 0;
	for(int i = 1; i <= n; i++){
		if(a[i] >= 0) sum += a[i]*(m-1)+max(a[i],b[i]);
		else sum += max(a[i]*m, b[i]);
		ans = max(ans, sum + s[i+1]-mn[i+1]);
	}
	cout << ans << endl;
	
	return 0;
}
0