Language: C++

#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int K;
    cin >> K;
    int  a[n];
    int k = n;;
    int t = 0;
    
    for(int i = 0;i<n;i++){
        cin >> a[i];
        if(a[i]>=0){
            if(a[i]> 0){
            k = i ; break;
            }
            else{
            t = 1;
            k = i;
            break;
            }
        }
        
    }
    for(int i = k+1;i<n;i++){
        cin >> a[i];
    }
    
    if(t == 0){
        int p = k-K;
        if(p<0){
            p = 0;
        }
        int m = 2147000000;
        
        for(int i = p;i<k;i++){
            if(K+i>n)break;
            if(m>(a[i]*(-2)+a[K+i-1])){
                m=a[i]*(-2)+a[K+i-1];
               
            }
            if(m>(a[i]*(-1)+2*a[K+i-1])){
                m=a[i]*(-1)+2*a[K+i-1];
               
            }
        }
        if(k+K<=n&& m>a[k+K-1]){
            m = a[k+K-1];
        }
        cout << m;
    }
    else{
        int p = k-K+1;
        if(p<0){
            p = 0;
        }
        int m = 2147000000;
        
        for(int i = p;i<k;i++){
            if(K+i>n)break;
            if(m>(a[i]*(-2)+a[K+i-1])){
                m=a[i]*(-2)+a[K+i-1];
               
            }
            if(m>(a[i]*(-1)+2*a[K+i-1])){
                m=a[i]*(-1)+2*a[K+i-1];
               
            }
        }
        if(k+K<=n&& m>a[k+K-1]){
            m = a[k+K-1];
        }
        cout << m;
    }
    
    
    return 0;
}
        int p = k-K+1;
        if(p<0){
            p = 0;
        }
        int m = 200002;
        
        for(int i = p;i<k;i++){
            if(K+i>n)break;
            if(m>(a[i]*(-2)+a[K+i-1])){
                m=a[i]*(-2)+a[K+i-1];
               
            }
            if(m>(a[i]*(-1)+2*a[K+i-1])){
                m=a[i]*(-1)+2*a[K+i-1];
               
            }
        }
        if(k+K<=n&& m>a[k+K-1]){
            m = a[k+K-1];
        }
        cout << m;
    }
    
    
    return 0;
}

Compiler output:
a.cc:85:17: error: 'k' was not declared in this scope
   85 |         int p = k-K+1;
      |                 ^
a.cc:85:19: error: 'K' was not declared in this scope
   85 |         int p = k-K+1;
      |                   ^
a.cc:86:9: error: expected unqualified-id before 'if'
   86 |         if(p<0){
      |         ^~
a.cc:91:9: error: expected unqualified-id before 'for'
   91 |         for(int i = p;i<k;i++){
      |         ^~~
a.cc:91:23: error: 'i' does not name a type
   91 |         for(int i = p;i<k;i++){
      |                       ^
a.cc:91:27: error: 'i' does not name a type
   91 |         for(int i = p;i<k;i++){
      |                           ^
a.cc:102:9: error: expected unqualified-id before 'if'
  102 |         if(k+K<=n&& m>a[k+K-1]){
      |         ^~
a.cc:105:9: error: 'cout' does not name a type
  105 |         cout << m;
      |         ^~~~
a.cc:106:5: error: expected declaration before '}' token
  106 |     }
      |     ^
a.cc:109:5: error: expected unqualified-id before 'return'
  109 |     return 0;
      |     ^~~~~~
a.cc:110:1: error: expected declaration before '}' token
  110 | }
      | ^