Problem p03545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int a[4];

bool Ok(string s) {
  int sum = a[0];
  for (int i = 0; i < 3; ++i) {
    if (s[i] == '+')
      sum += a[i + 1];
    else
      sum -= a[i + 1];
  }
  return sum == 7;
}

int main() {
  for (int i = 0; i < 4; ++i) {
    char c;
    cin >> c;
    a[i] = c - '0';
  }
  char c[2] = {'+', '-'};
  string s = "   ";
  for (int i = 0; i < 2; ++i) {
    s[0] = c[i];
    for (int j = 0; j < 2; ++j) {
      s[1] = c[j];
      for (int k = 0; k < 2; ++k) {
        s[2] = c[k];
        if (Ok(s)) {
          for (int l = 0; l < 3; ++l) {
            cout << a[l] << c[l];
          }
          cout << a[3] << '=' << 7 << endl;
          return 0;
        }
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> using umap = unordered_map<T, U>;
template <class T> using uset = unordered_set<T>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int a[4];

bool Ok(string s) {
  int sum = a[0];
  for (int i = 0; i < 3; ++i) {
    if (s[i] == '+')
      sum += a[i + 1];
    else
      sum -= a[i + 1];
  }
  return sum == 7;
}

int main() {
  for (int i = 0; i < 4; ++i) {
    char c;
    cin >> c;
    a[i] = c - '0';
  }
  char c[2] = {'+', '-'};
  string s = "   ";
  for (int i = 0; i < 2; ++i) {
    s[0] = c[i];
    for (int j = 0; j < 2; ++j) {
      s[1] = c[j];
      for (int k = 0; k < 2; ++k) {
        s[2] = c[k];
        if (Ok(s)) {
          for (int l = 0; l < 3; ++l) {
            cout << a[l] << s[l];
          }
          cout << a[3] << '=' << 7 << endl;
          return 0;
        }
      }
    }
  }
}