What's wrong in my solution of SHROUT

I found the logic to be correct, what am i missing?

#include <bits/stdc++.h>
using namespace std;
long long int minimum(long long int x, long long int y){
if (x < y) return x;
else return y;
}

void solve(){
long long int n,m,counter;
scanf("%lli %lli", &n, &m);
long long int cities[n],lengths[n],visitors[m];

for(long long int i = 0 ; i < n; i++){
    scanf("%lli", &cities[i]);
}

for(long long int i = 0 ; i < m; i++){
    scanf("%lli", &visitors[i]);
}


counter = -1;
for(long long int i = n-1; i >= 0; i--){
    if(cities[i] == 2) counter = 0;
    lengths[i] = counter;
    if(counter != -1) counter++;
}
counter = -1;
for(long long int i = 0 ; i < n; i++){
    if(cities[i] == 1) counter = 0;
    if(lengths[i] != -1){
        if(counter != -1) lengths[i] = minimum(lengths[i],counter);
    }
    else lengths[i] = counter;
    if(counter != -1) counter++;
}

cout << lengths[visitors[0]-1];
for(long long int i = 1 ; i < m; i++){
    cout << " " << lengths[visitors[i] - 1];
}
cout << endl;

}
int main(){
long long int t;
cin >> t;
while(t–){
solve();
}
return 0;
}

You would get wrong answer for city 1. lengths[0] should always be 0.

What an annoying mistake, thanks for the immediate feedback :+1:t4: