Bismillahir Rahmanir Rahim
///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b) N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;
///Bismillahir Rahmanir Rahim
///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b) N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
ll tst,n,k;
cin>>tst;
while(tst--)
{
cin>>n>>k;
ll sum=0,arr[n+7],sol,pro;
for(ll i=1; i<=n; i++)
cin>>arr[i],sum+=arr[i];
if(sum>k)
{
ll ans[n+9],pos=1,cnt=0,k1=sum-k;
ans[0]=0;
for(ll i=1;i<=n && k1!=0;i++)
if(arr[i]) ans[pos++]=i,k1--;
sol=ans[pos-1],pro=pos-1;
for(ll i=n;i>=1;i--)
{
cnt++;
if(arr[i])
{
sol=min(sol,cnt+ans[pro-1]);
pro--;
if(pro==0) break;
}
}
cout<<sol<<endl;
}
else if(sum==k) cout<<"0"<<endl;
else cout<<"-1"<<endl;
}
return 0;
}
///Alhamdulillah
Problem Link: https://codeforces.com/contest/1692/problem/E
Comments
Post a Comment