Problem p00082 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int jenny[8] = {4, 1, 2, 1, 2, 1, 4, 1};
int main() {
  int p[7];
  int sum;
  int pi;
  while (scanf("%d %d %d %d %d %d %d %d", &p[0], &p[1], &p[2], &p[3], &p[4],
               &p[5], &p[6], &p[7])) {
    if (cin.eof())
      break;
    int ans = INF;
    for (int i = 0; i <= 7; i++) { // どれだけずらすか
      sum = 0;
      for (int j = 0; j <= 7; j++) {
        int d = jenny[(i + j) % 8] - p[j];
        if (d < 0) {
          sum += abs(d);
        }
      }
      if (ans == sum) {
        int d1 = 0;
        int d2 = 0;
        for (int j = 0; j <= 7; j++) {
          d1 += jenny[(j + pi) % 8] * pow(10.0, 7 - j);
        }
        for (int j = 0; j <= 7; j++) {
          d2 += jenny[(j + i) % 8] * pow(10.0, 7 - j);
        }
        if (d1 > d2) {
          pi = i;
          continue;
        }
        continue;
      }
      if (ans > sum) {
        ans = sum;
        pi = i;
      }
    }
    for (int i = 0; i <= 7; i++) {
      cout << jenny[(i + pi) % 8];
      if (i != 7)
        cout << " ";
      if (i == 7)
        cout << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int jenny[8] = {4, 1, 2, 1, 2, 1, 4, 1};
int main() {
  int p[7];
  int sum;
  int pi;
  while (scanf("%d %d %d %d %d %d %d %d", &p[0], &p[1], &p[2], &p[3], &p[4],
               &p[5], &p[6], &p[7]) != EOF) {
    if (cin.eof())
      break;
    int ans = INF;
    for (int i = 0; i <= 7; i++) { // どれだけずらすか
      sum = 0;
      for (int j = 0; j <= 7; j++) {
        int d = jenny[(i + j) % 8] - p[j];
        if (d < 0) {
          sum += abs(d);
        }
      }
      if (ans == sum) {
        int d1 = 0;
        int d2 = 0;
        for (int j = 0; j <= 7; j++) {
          d1 += jenny[(j + pi) % 8] * pow(10.0, 7 - j);
        }
        for (int j = 0; j <= 7; j++) {
          d2 += jenny[(j + i) % 8] * pow(10.0, 7 - j);
        }
        if (d1 > d2) {
          pi = i;
          continue;
        }
        continue;
      }
      if (ans > sum) {
        ans = sum;
        pi = i;
      }
    }
    for (int i = 0; i <= 7; i++) {
      cout << jenny[(i + pi) % 8];
      if (i != 7)
        cout << " ";
      if (i == 7)
        cout << endl;
    }
  }
  return 0;
}