20250901 baekjoon

This commit is contained in:
songyc macbook 2025-09-01 16:22:07 +09:00
parent d6a492a687
commit 234b3de4a9

View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
int max(int a, int b) {
return a>b ? a:b;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
int n;
scanf("%d",&n);
int** sticker = (int**)malloc(sizeof(int*)*2);
int** dp = (int**)malloc(sizeof(int*)*2);
for(int i=0; i<2; i++) {
sticker[i] = (int*)malloc(sizeof(int)*n);
dp[i] = (int*)malloc(sizeof(int)*n);
for(int j=0; j<n; j++) {
scanf("%d", &sticker[i][j]);
dp[i][j] = 0;
}
}
dp[0][0] = sticker[0][0];
dp[1][0] = sticker[1][0];
if(n==1) printf("%d\n",max(dp[0][0], dp[1][0]));
else {
dp[0][1] = sticker[1][0] + sticker[0][1];
dp[1][1] = sticker[0][0] + sticker[1][1];
for(int i=2; i<n; i++) {
dp[0][i] = sticker[0][i] + max(dp[1][i-1],dp[1][i-2]);
dp[1][i] = sticker[1][i] + max(dp[0][i-1],dp[0][i-2]);
}
printf("%d\n",max(dp[0][n-1],dp[1][n-1]));
for(int i=0; i<2; i++) {
free(sticker[i]);
free(dp[i]);
}
free(sticker);
free(dp);
}
}
return 0;
}