It does take more time to reallocate memory every time, but code simplicity and readability are a lot more important than the minimal decrease in time taken. I don’t think I’ve ever gotten a TLE for declaring variables in the lowest scope.
1 Like
#include <stdio.h>
int main()
{
long int n,m,i,j,z,t,min,max;
scanf("%ld",&t);
for(z=0;z<t;z++)
{
scanf("%ld %ld",&n,&m);
int a[n],b[n];
int c[60]={-1};
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
max=0;
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
min=100000;
for(j=0;j<n;j++)
{
if(a[j]>0 && a[j]<=m && c[a[j]]==-1)
c[a[j]]=0;
if(c[a[j]]!=-1)
c[a[j]]+=b[j];
}
for(i=0;i<=max;i++)
{
if(c[i]!=-1)
{
if(c[i]<min)
{ min=c[i];
}
}
}
printf("%ld\n",min);
}
}
what is wrong in my solution? please help