#include<stdio.h>
//#include<conio.h>
#include<math.h>
char digits[100003]; //storeing input string
int dig[100003][2]; //if dig[i][0]=1,it means we have the value of indices i in dig[i][1]
int num[10][2]; //
void update(int I,int ans) //for update the value of dig[][]
{
dig[I][0]=1;
dig[I][1]=ans;

}
int Ans(int i,int j)
{ int ans=0,k,l1,l2;
l1=digits[j+2]-48;
for(k=i;k<=j;k++)
{ l2=digits[k+1]-48;
ans+=abs(l1-l2);
}
return ans;
}
int main()
{
int i,j,m,n,I,p,p1,p2,ans,ans1,In;
scanf("%d%d",&n,&m);

for(i=1;i<=n+1;i++)
scanf("%c",&digits[i]);

while(m--)
{
scanf("%d",&I);
In=digits[I+1]-48;

//	printf("%d\n",dig[I][0]);
if(dig[I][0]==1)
{
printf("\n%d",dig[I][1]);
continue;
}
else{

if(num[In][0]==1)
{
p=num[In][1];
if(I>p)
{
ans1=Ans(p+1,I-1);
ans=dig[p][1]+ans1;
printf("\n%d",ans);
update(I,ans);
num[In][1]=I;
continue;
}
else
{
p1=p-I;
p2=I-0;
if(p1<p2)
{
ans1=Ans(I+1,p-1);
ans=dig[p][1]-ans1;
printf("\n%d",ans);
update(I,ans);
num[In][1]=I;
continue;
}
else
{
ans1=Ans(1,I-1);
ans=ans1;
printf("\n%d",ans);
update(I,ans);
num[In][1]=I;
continue;
}
}
}
else
{
ans1=Ans(1,I-1);
ans=ans1;
printf("\n%d",ans1);
update(I,ans);
num[digits[I]][0]=1;
continue;

}

}
}

return 0;
}

How do you think anyone will be able to understand what you are doing. Before asking you should write a nicely commented code.

Terima kasih dan salam kenal.