Problem p03276 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
#define all(A) A.begin(), A.end()
typedef unsigned long long ull;
const int inf = 1039074182;
using namespace std;
int n, k;
int a[100005];
vector<int> l;
vector<int> r;

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  int p = 0;
  r.push_back(0);
  while (a[p] < 0 && p < n) {
    l.push_back(-a[p]);
    p++;
  }
  if (p != n && a[p] == 0)
    k--, p++;
  while (p != n) {
    //		cout<<p<<endl;
    r.push_back(a[p]);
    //		p++;
  }
  l.push_back(0);
  reverse(l.begin(), l.end());
  //	for(int i=0;i<l.size();i++)
  //	{
  //		cout<<l[i]<<' ';
  //	}
  //	cout<<endl;
  //	for(int i=0;i<r.size();i++)
  //	{
  //		cout<<r[i]<<' ';
  //	}
  //	cout<<endl;
  int res = inf;
  for (int i = 0; i <= k; i++) // choose from l
  {
    if (l.size() - 1 < i)
      break;
    if (k - i > r.size() - 1)
      continue;
    res = min(res, l[i] * 2 + r[k - i]);
    res = min(res, l[i] + 2 * r[k - i]);
    if (i == 0)
      res = min(res, r[k - i]);
    if (i == k)
      res = min(res, l[i]);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
#define all(A) A.begin(), A.end()
typedef unsigned long long ull;
const int inf = 1039074182;
using namespace std;
int n, k;
int a[100005];
vector<int> l;
vector<int> r;

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  int p = 0;
  r.push_back(0);
  while (a[p] < 0 && p < n) {
    l.push_back(-a[p]);
    p++;
  }
  if (p != n && a[p] == 0)
    k--, p++;
  while (p != n) {
    //		cout<<p<<endl;
    r.push_back(a[p]);
    p++;
  }
  l.push_back(0);
  reverse(l.begin(), l.end());
  //	for(int i=0;i<l.size();i++)
  //	{
  //		cout<<l[i]<<' ';
  //	}
  //	cout<<endl;
  //	for(int i=0;i<r.size();i++)
  //	{
  //		cout<<r[i]<<' ';
  //	}
  //	cout<<endl;
  int res = inf;
  for (int i = 0; i <= k; i++) // choose from l
  {
    if (l.size() - 1 < i)
      break;
    if (k - i > r.size() - 1)
      continue;
    res = min(res, l[i] * 2 + r[k - i]);
    res = min(res, l[i] + 2 * r[k - i]);
    if (i == 0)
      res = min(res, r[k - i]);
    if (i == k)
      res = min(res, l[i]);
  }
  cout << res << endl;
  return 0;
}