Language: C++

#include <bits/stdc++.h>
using namespace std;
bool a[5000], b[5000];
int n;
int ans;
int pre[5000], after[5000];
int rangetree[20000][2];
void push(int curr)
{
	rangetree[2*curr][1] = max(rangetree[2*curr][1], rangetree[curr][1]);
	rangetree[2*curr][0] = max(rangetree[2*curr][0], rangetree[curr][1]);
	rangetree[2*curr+1][1] = max(rangetree[2*curr+1][1], rangetree[curr][1]);
	rangetree[2*curr+1][0] = max(rangetree[2*curr+1][0], rangetree[curr][1]);
}
void update(int node, int val, int curr = 1, int cstart = 0, int cend = 2200)
{

	if (cstart == cend)
	{
		rangetree[curr][0] = max(val, rangetree[curr][1]);
		return;
	}
	push(curr);
	int mid = (cstart+cend)/2;
	if (node <= mid)
	{
		update(node, val, 2*curr, cstart, mid);
	}
	else update(node, val, 2*curr+1, mid+1, cend);
	rangetree[curr][0] = min(rangetree[2*curr][0], rangetree[2*curr+1][0]);
}
void updaterange(int start, int end, int val, int curr = 1, int cstart = 0, int cend = 2200)
{
	//printf("%d %d %d %d\n", start, end, cstart, cend);
	if (start <= cstart && cend <= end)
	{
		rangetree[curr][1] = max(rangetree[curr][1], val);
		rangetree[curr][0] = max(rangetree[curr][1], rangetree[curr][0]);
		return;
	}
	int mid = (cstart+cend)/2;
	push(curr);
	if (end <= mid) updaterange(start, end, val, 2*curr, cstart, mid);
	else if (start > mid) updaterange(start, end, val, 2*curr+1, mid+1, cend);
	else
	{
		updaterange(start, end, val, 2*curr, cstart, mid);
		updaterange(start, end, val, 2*curr+1, mid+1, cend);
	}
	rangetree[curr][0] = min(rangetree[2*curr][0], rangetree[2*curr+1][0]);
}
int query(int start, int end, int curr = 1, int cstart = 0, int cend = 2200)
{
	if (start <= cstart && cend <= end)
	{
		return rangetree[curr][0];
	}
	int mid = (cstart+cend)/2;
	push(curr);
	if (end <= mid) return query(start, end, 2*curr, cstart, mid);
	else if (start > mid) return query(start, end, 2*curr+1, mid+1, cend);
	else
	{
		return min(query(start, end, 2*curr, cstart, mid), query(start, end, 2*curr+1, mid+1, cend));
	}
}
void doit()
{
	int newa[5000];
	int p = -1;
	for (int i = 0; i < n; i++)
	{
		if (b[i]) p = i;
		pre[i] = p;
	}
	p = -1;
	for (int i = n-1; i >= 0; i--)
	{
		if (b[i]) p = i;
		after[i] = p;
	}
	for (int i = 0; i < n; i++)
	{
		bool done = false;
		int currans = 0;
		for (int i = 0; i < 10000; i++)
		{
			rangetree[i][0] = 999999999;
			rangetree[i][1] = 0;
		}
		for (int j = 0; j < n; j++)
		{
			int newj = j-i;
			if (newj < 0) newj = n+newj;
			newa[j] = a[newj];
		//	printf("%d ", newa[j]);
			if (newa[j] == b[j]) continue;
			currans++;
			int prev = pre[j];
			int adiff, bdiff;
			if (prev != -1)
			{
				adiff = j-prev;
			//	printf("hi ");
			}
			else
			{
				adiff = j+(n-pre[n-1]);
			}
			prev = after[j];
			if (prev != -1)
			{
				bdiff = prev-j;
			}
			else
			{
				bdiff = n-j+after[0];
			}
		//	printf("%d %d\n", adiff, bdiff);
			if (adiff <= i)
			{
				continue;
			}
			adiff-=i;		
			if (!done)
			{
				done = true;
				update(adiff, 0);
				update(0, bdiff);
				updaterange(0, adiff-1, bdiff);
				continue;
			}
			update(adiff, query(0, adiff));
			if (adiff) updaterange(0, adiff-1, bdiff);

		}
		
		int mn = 999999999;
		int am = 999999999;
		for (int j = 0; j < n; j++) { /*printf(" %d", query(j, j));*/ am = mn(am, query(j, j)); mn = min(mn, j+am); }
		if (!done) mn = 0;
		ans = min(ans, 2*mn+currans+i);
	}
}
int main()
{
	char inp[5000];
	scanf(" %s", inp);
	n = strlen(inp);
	for (int i = 0; i < n; i++) a[i] = inp[i]-'0';
	scanf(" %s", inp);
	for (int i = 0; i < n; i++) b[i] = inp[i]-'0';
	ans = 999999999;
	bool isone = false;
	for (int i = 0; i < n; i++)
	{
		if (b[i]) isone = true;
	}
	if (!isone)
	{
		printf("-1\n");
		return 0;
	}
	doit();
	reverse(a, a+n);
	reverse(b, b+n);
	doit();
	printf("%d\n", ans);
}















Compiler output:
a.cc: In function 'void doit()':
a.cc:140:86: error: 'mn' cannot be used as a function
  140 |                 for (int j = 0; j < n; j++) { /*printf(" %d", query(j, j));*/ am = mn(am, query(j, j)); mn = min(mn, j+am); }
      |                                                                                    ~~^~~~~~~~~~~~~~~~~