20250901 baekjoon
This commit is contained in:
parent
d6a492a687
commit
234b3de4a9
50
code_study/Baekjoon/c/9465.c
Normal file
50
code_study/Baekjoon/c/9465.c
Normal 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user