結果

問題 No.2139 K Consecutive Sushi
ユーザー kotatsugame
提出日時 2022-12-03 00:54:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 509 bytes
コンパイル時間 1,036 ms
コンパイル使用メモリ 77,592 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-11-27 19:48:51
合計ジャッジ時間 3,284 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    8 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<atcoder/segtree>
using namespace std;
long op(long a,long b){return max(a,b);}
long e(){return -1e18;}
int N,K,A[2<<17];
long S[2<<17];
main()
{
	cin>>N>>K;
	K--;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		S[i+1]=S[i]+A[i];
	}
	atcoder::segtree<long,op,e>seg(N+2);
	seg.set(0,0L);
	long ans=0;
	for(int i=1;i<=N;i++)
	{
		long now=seg.prod(max(i-K,0),i)+S[i];
		ans=max(ans,now);
		seg.set(i,op(seg.get(i),seg.get(i-1)+S[i-1]-S[i]));
		seg.set(i+1,now-S[i+1]);
	}
	cout<<ans<<endl;
}
0