What is wrong in my approach. It is failing for one test case on submitting. Any help would help me a lot. My approach goes this way- I have calculated time required for zero elements to become non-zero and then finally traversed this array to calculate sum by adding 2*max(0, ans[i]) for every i in ans array which contains t for every element.
#include <bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define pii pair<int, int>
#define vii vector
#define make make_pair
#define f first
#define s second
#define si unordered_set
#define sll unordered_set
#define mii unordered_map<int, int>
#define mll unordered_map<ll, ll>
#define ll long long
#define vi vector
#define vll vector
#define maxpq priority_queue
#define minpq priority_queue<int, vector, greater >
#define MOD (int) 1e9+7
#define take(n) int n; cin >> n
void pr(int x) {cout << x;}
void prl(int x) {cout << x << endl;}
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define tc(t) int t; cin >> t; while (t–)
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL),cout.tie(NULL)
#define prec(n) fixed<<setprecision(n)
#define ini(a, i) memset(a, i, sizeof(a))
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
int max(int a, int b) {if (a > b) return a; else return b;}
int min(int a, int b) {if (a < b) return a; else return b;}
const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const int N = (int)(5 * 1e5 + 10);
vector<vector> divs(N);
void pre(){
int i, j;
for1(i, N-1){
for(int j=i;j<N;j+=i)
divs[j].pb(i);
}
}
ll add(ll x, ll y) {ll res=x+y; return res>=MOD ? res-MOD:res;}
ll mul(ll x, ll y) {ll res=x*y; return res>=MOD? res%MOD:res;}
ll sub(ll x, ll y) {ll res=x-y; return res<0? res+MOD:res;}
ll power(ll x, ll y) {ll res=1; x%=MOD; while(y){ if (y&1) res=mul(res, x); y >>=1; x=mul(x, x);} return res;}
ll mod_ind(ll x) {return power(x, MOD-2);}
int main(){
//#ifndef ONLINE_JUDGE
//freopen(“input.txt”, “r”, stdin);
//freopen(“output.txt”, “w”, stdout);
//#endif
tc(t){
take(n); take(k);
int arr[n];
bool flag=false;
int start;
ll sum=0;
for0(i, n){
cin >> arr[i];
sum+=arr[i];
if (arr[i]>0){
if (flag==false)
start=i;
flag=true;
}
}
if (flag==false){
cout << "0\n";
continue;
}
vi temp(n, 0); // to store time for zeroes to become non-zero.
int count=1;
for (int i=start+1;i!=start;i=(i+1)%n){ // first traversing left to right & putting counts in temp.
if (arr[i]==0){
temp[i]=count;
count++;
}
else{
count=1;
}
}
count=1;
for (int i=start-1;i!=start;i=(i-1+n)%n){ // now traversing right to left and updating temp only if value is smaller.
if (arr[i]==0){
temp[i]=min(temp[i], count);
count++;
}
else
count=1;
}
// now temp contains time taken for zero elements to become non-zero.
// for (auto &i: temp){
// sum=sum+2*max(0, k-i);
// }
cout << sum << endl;
}
return 0;
}