JOI2007本選 問題1@C++
瞬殺。というかそうじゃないとまずいよね。
#include <climits> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main(int argc, char *argv[], char *envp[]) { #ifdef DEBUG FILE *in = stdin; FILE *out = stdout; #else FILE *in = fopen("input.txt", "r"); FILE *out = fopen("output.txt", "w"); #endif int n; int k; fscanf(in, "%d %d\n", &n, &k); vector<int> a(n); for(int i=0;i<n;i++) { fscanf(in, "%d\n", &a[i]); } int subsum = 0; int max_sum = INT_MIN; for(int i=0;i<k;i++) { subsum += a[i]; } for(int i=k;i<n;i++) { max_sum = max(max_sum, subsum); subsum += a[i]; subsum -= a[i-k]; } max_sum = max(max_sum, subsum); fprintf(out, "%d\n", max_sum); fclose(in); fclose(out); return 0; }