Submission #67795356


Source Code Expand

Copy
#include <stdio.h>
#include <inttypes.h>
int N, K;
int A[251234];
int64_t sum[251234];
int64_t memo[251234][2];
int64_t calc(int pos, int can_one) {
int64_t ans;
if (pos > N) return 0;
if (memo[pos][can_one]) return ~memo[pos][can_one];
/* */
ans = calc(pos + 1, 0);
/* */
if (can_one) {
int64_t candidate = calc(pos + 1, 1) + A[pos];
if (candidate > ans) ans = candidate;
} else if (pos + K - 1 <= N) {
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <inttypes.h>

int N, K;
int A[251234];

int64_t sum[251234];

int64_t memo[251234][2];

int64_t calc(int pos, int can_one) {
	int64_t ans;
	if (pos > N) return 0;
	if (memo[pos][can_one]) return ~memo[pos][can_one];
	/* 食べない */
	ans = calc(pos + 1, 0);
	/* 食べる */
	if (can_one) {
		int64_t candidate = calc(pos + 1, 1) + A[pos];
		if (candidate > ans) ans = candidate;
	} else if (pos + K - 1 <= N) {
		int64_t candidate = calc(pos + K, 1) + sum[pos + K - 1] - sum[pos - 1];
		if (candidate > ans) ans = candidate;
	}
	return ~(memo[pos][can_one] = ~ans);
}

int main(void) {
	int i;
	if (scanf("%d%d", &N, &K) != 2) return 1;
	for (i = 1; i <= N; i++) {
		if (scanf("%d", &A[i]) != 1) return 1;
		sum[i] = sum[i - 1] + A[i];
	}
	printf("%" PRId64 "\n", calc(1, 0));
	return 0;
}

/*

1回食べれば、そのあとは1ずつ範囲をずらすことで1ずつ食べることができる

[位置][1個ずつ進められるか]でメモ化探索

*/

Submission Info

Submission Time
Task A - Apple Addiction
User mikecat
Language C (gcc 12.2.0)
Score 300
Code Size 1037 Byte
Status AC
Exec Time 44 ms
Memory 28104 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 4
AC × 21
Set Name Test Cases
Sample 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt
All 00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 00-sample-004.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt
Case Name Status Exec Time Memory
00-sample-001.txt AC 1 ms 1732 KiB
00-sample-002.txt AC 1 ms 1732 KiB
00-sample-003.txt AC 0 ms 1632 KiB
00-sample-004.txt AC 0 ms 1572 KiB
01-001.txt AC 0 ms 1632 KiB
01-002.txt AC 1 ms 1656 KiB
01-003.txt AC 0 ms 1596 KiB
01-004.txt AC 1 ms 1680 KiB
01-005.txt AC 4 ms 3696 KiB
01-006.txt AC 9 ms 7272 KiB
01-007.txt AC 10 ms 7424 KiB
01-008.txt AC 42 ms 28008 KiB
01-009.txt AC 42 ms 28096 KiB
01-010.txt AC 43 ms 28008 KiB
01-011.txt AC 43 ms 27928 KiB
01-012.txt AC 44 ms 28012 KiB
01-013.txt AC 44 ms 28012 KiB
01-014.txt AC 44 ms 28104 KiB
01-015.txt AC 42 ms 28104 KiB
01-016.txt AC 41 ms 27932 KiB
01-017.txt AC 40 ms 27964 KiB


2025-07-21 (Mon)
07:30:41 +09:00