#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+1<N; i++)
ans+= (R[i]>L[i+1]) ? L[i+1] : R[i];
printf("%d\n", ans);
}
return 0;
}
Hey, the problem with your code is that you are considering both i and i+1 everytime, while there can be cases where due to previous iteration, the index i has been removed.
Thank you, but could you tell how this can be fixed?
hey @khusniddin
In your solution there is the problem of integer overflow try using long long.
You can refer to this solution :
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
cin>>t;
while(t--)
{
ll n;
cin>>n;
vector<ll> a(n),b(n);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>b[i];
ll sum= 0;
for(int i=1;i<n;i++)
{
sum+=min(b[i-1],a[i]);
}
cout<<sum<<"\n";
}
}