Could anybody explain what is wrong with this code? It is running fine in a sample test, but...

#include <stdio.h>

int main(void) {
int T, N;
scanf("%d", &T);

while (T--) {
    scanf("%d", &N);
    int L[N], R[N];
    for (int i=0; i<N; i++) scanf("%d", &L[i]);
    for (int i=0; i<N; i++) scanf("%d", &R[i]);

    int ans=0;
        for (int i=0; i<N; i++) 
            if (i+1<N)
                ans+= (R[i]>L[i+1]) ? L[i+1] : R[i]; 
    printf("%d\n", ans);    

return 0;


Hey, could you mention the question name/link for which you have written the solution? Also, to get a faster response, you should try and write down your logic along with the implementation, which makes it is easier for users to understand your approach and find possible flaws in it.

Contest Page | CodeChef, thank you…

Hey, there can be cases where elements at either i or i+1 index may not pre present, because it has been removed in a previous operation, but in your code, you are still considering it.

Thank you, but could you tell how this can be fixed?