Language: C

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void){
    // Your code here!
    int key = 0, flag = 0, A[200000], N, max = 0, min = 1000000000;
    long sumlleft, sumrright, sumleft, sumcandi, sumright, candi = 0, Min = LONG_MAX;
    scanf("%d",&N);
    for(int i = 1; i <= N; i++)
    {
        scanf("%d",&A[i-1]);
        A[i-1] = A[i-1] - i;
        
        if(A[i-1] > max) max = A[i-1];
        if(A[i-1] < min) min = A[i-1];
        
        candi += A[i-1];
    }
    
    candi /= N;
    
    while(flag == 0)
    {
        switch(key)
        {
            case 0:
                sumlleft = sumrright = sumleft = sumcandi = sumright = 0;
                break;
            case 1:
                sumright = 0;
                break;
            case -1:
                sumleft = 0;
                break;
            case 2:
                sumright = sumlleft = sumrright = 0;
            case -2:
                sumleft = sumlleft = sumrright = 0;
        }
        
        for(int j = 0; j < N; j++)
        {
            switch(key)
            {
                case 0:
                    sumcandi += labs(A[j] - candi);
                    sumleft += labs(A[j] - candi - 1);
                    sumright += labs(A[j] - candi + 1);
                    break;
                    
                case 1:
                    sumright += labs(A[j] - candi + 1);
                    break;
                    
                case -1:
                    sumleft += labs(A[j] - candi - 1);
                    break;
                    
                case 2:
                    sumright += labs(A[j] - candi + 1);
                    break;
                    
                case -2:
                    sumleft += labs(A[j] - candi - 1);
                    break;
                    
                    
            }
        }
        //printf("%ld %ld %ld\n", sumleft, sumcandi, sumright);
        
        if(sumcandi <= sumright && sumleft <= sumcandi)
        {
            if(key == 1)
            {
                Min = sumcandi;
                flag = 1;
            }
            else
            {
                candi--;
                key = -1;
                sumright = sumcandi;
                sumcandi = sumleft;
            }
        }
        else if(sumcandi >= sumright && sumleft >= sumcandi)
        {
            if(key == -1)
            {
                Min = sumcandi;
                flag = 1;
            }
            else
            {
                candi++;
                key = 1;
                sumleft = sumcandi;
                sumcandi = sumright;
            }
        }
        else if(sumcandi >= sumright && sumleft <= sumcandi)
        {
            if(sumleft < sumright)
            {
                if(key == 1)
                {
                    Min = sumcandi;
                    flag = 1;
                }
                else
                {
                    candi--;
                    key = -1;
                    sumright = sumcandi;
                    sumcandi = sumleft;
                }
            }
            else if(sumleft > sumright)
            {
                if(key == -1)
                {
                    Min = sumcandi;
                    flag = 1;
                }
                else
                {
                    candi++;
                    key = 1;
                    sumleft = sumcandi;
                    sumcandi = sumright;
                }
            }
            else
            {
                for(int k = 0; k < N; k++)
                {
                    sumlleft += labs(A[j] - candi - 2);
                    sumrright += labs(A[j] - candi + 2);
                }
                if(sumlleft < sumrright)
                {
                    candi -= 2;
                    sumcandi = sumlleft;
                    sumright = sumleft;
                    key = -2;
                }
                else
                {
                    candi += 2;
                    sumcandi = sumrright;
                    sumleft = sumright;
                    key = 2;
                }
            }
        }
        else
        {
            flag = 1;
            Min = sumcandi;
        }
    }
    
    printf("%ld\n",Min);
}


Compiler output:
main.c: In function 'main':
main.c:139:40: error: 'j' undeclared (first use in this function)
  139 |                     sumlleft += labs(A[j] - candi - 2);
      |                                        ^
main.c:139:40: note: each undeclared identifier is reported only once for each function it appears in