Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int, int> PII;
typedef long long LL;

#define d(x) cout << #x << " = " << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REACH(i, c)                                                            \\
  for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c, n) sort(c, c + n)
#define VSORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define DFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define DREP(i, n) DFOR(i, n, 0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI = acos(-1.0);
////////////////////////////////////////////////
int s;
char c;
int main() {
  for (;;) {
    for (int i = 0;; i++) {
      scanf("%c", &c);
      if (i == 0 && c == '0')
        return 0;
      else if (c == '\\n')
        break;
      s += atoi(&c);
    }
    cout << s << endl;
    s = 0;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int, int> PII;
typedef long long LL;

#define d(x) cout << #x << " = " << (x) << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REACH(i, c)                                                            \\
  for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c, n) sort(c, c + n)
#define VSORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define DFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define DREP(i, n) DFOR(i, n, 0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI = acos(-1.0);
////////////////////////////////////////////////
int s;
char c;
int main() {
  for (;;) {
    for (;;) {
      scanf("%c", &c);
      if (c == EOF)
        return 0;
      else if (c == '\\n')
        break;
      s += atoi(&c);
    }
    cout << s << endl;
    s = 0;
  }
}