Problem p03274 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {

  int n, k, i, begin_pos = 2147483647, left_pos, right_pos, left, right,
               totalWalk, totalWalkFL = 0, totalWalkFR = 0,
               totalWalk_min = 2147483647;
  cin >> n >> k;
  int x[n];
  for (i = 0; i < n; i++) {
    cin >> x[i];
    if (x[i] >= 0 && begin_pos == 2147483647) {
      begin_pos = i;
    }
  }

  for (i = 0; i <= k; i++) {
    left_pos = begin_pos - i;
    right_pos = left_pos + k - 1;
    if (left_pos < 0 || right_pos >= n) {
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | Error" << endl;
      continue;
    }

    left = abs(x[left_pos]);
    right = x[right_pos];

    if (i == 0) {
      totalWalk = right;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalk" << totalWalk << endl;
    } else if (i == k) {
      totalWalk = left;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalk" << totalWalk << endl;
    } else {
      totalWalkFL = left * 2 + right;
      totalWalkFR = left + right * 2;
      totalWalk = totalWalkFL < totalWalkFR ? totalWalkFL : totalWalkFR;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalkFL:" << totalWalkFL << "
      //totalWalkFR:" << totalWalkFR << endl;
    }

    if (totalWalk < totalWalk_min) {
      totalWalk_min = totalWalk;
    }
  }

  cout << totalWalk_min << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int n, k, i, begin_pos = 2147483647, left_pos, right_pos, left, right,
               totalWalk, totalWalkFL = 0, totalWalkFR = 0,
               totalWalk_min = 2147483647;
  cin >> n >> k;
  int x[n];
  for (i = 0; i < n; i++) {
    cin >> x[i];
    if (x[i] >= 0 && begin_pos == 2147483647) {
      begin_pos = i;
    }
  }
  if (begin_pos == 2147483647) {
    begin_pos = n; // When only exists minus coodinates.
  }

  for (i = 0; i <= k; i++) {
    left_pos = begin_pos - i;
    right_pos = left_pos + k - 1;
    if (left_pos < 0 || right_pos >= n) {
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | Error" << endl;
      continue;
    }

    left = abs(x[left_pos]);
    right = x[right_pos];

    if (i == 0) {
      totalWalk = right;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalk" << totalWalk << endl;
    } else if (i == k) {
      totalWalk = left;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalk" << totalWalk << endl;
    } else {
      totalWalkFL = left * 2 + right;
      totalWalkFR = left + right * 2;
      totalWalk = totalWalkFL < totalWalkFR ? totalWalkFL : totalWalkFR;
      //			cout << "i:" << i << " LeftPos:" << left_pos <<
      //" RightPos:" << right_pos << " | totalWalkFL:" << totalWalkFL << "
      //totalWalkFR:" << totalWalkFR << endl;
    }

    if (totalWalk < totalWalk_min) {
      totalWalk_min = totalWalk;
    }
  }

  cout << totalWalk_min << endl;

  return 0;
}