Problem p03438 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define read() freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
#define sf(n) scanf("%d", &n)
#define sfd(n) scanf("%lf", &n)
#define sff(n, m) scanf("%d%d", &n, &m)
#define sl(x) scanf("%I64d", &x)
#define sfl(n) scanf("%lld", &n)
#define sfc(n) scanf(" %c", &n)
#define sffl(n, m) scanf("%lld%lld", &n, &m)
#define sful(n) scanf("%ull", &n)
#define ll long long
#define ull unsigned long long int
#define endll pf("\n")
#define pf printf
#define pi acos(-1.0)
#define eps 1e-10
#define mem(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define xx first
#define yy second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define inf 1999999999
#define MP make_pair
#define MX 100005
#define mod 1000000007
#define var int i, j, k, n, m, z = 0, c = 0, t, x, y

/*
ll bigmod(ll x,ll y,ll m)
{
    if(y==0)return 1;
    ll n=bigmod(x,y/2,m);
    n=(n*n)%mod;
    if(y & 1)n=((n%m)*(x%m))%m;
    return n;
}
*/

int main() {
  ll var;
  cin >> n;
  ll int a[n + 1], b[n + 1];
  z = 0, c = 0, k = 0;
  for (i = 0; i < n; i++) {
    sfl(a[i]);
  }
  for (j = 0; j < n; j++)
    sfl(b[j]);

  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      if (a[i] > b[i]) {
        k = a[i] - b[i];
        // cout<<c<<endl;
        if (z > 0) {
          if (k <= z) {
            z = z - k;
            b[i] += k;
          } else {
            b[i] += z;
            z = 0;
          }
          k = a[i] - b[i];
        }
        c += k;
        // cout<<k<<" "<<z<<endl;
      } else {
        k = b[i] - a[i];
        if (k & 1) {
          k++;
          c++;
          b[i] += 1;
        }
        if (c > 0) {
          if (k <= (c * 2)) {
            c = c - (k / 2);
            a[i] += k;
          } else {
            a[i] += (c * 2);
            c = 0;
          }
        }
        k = b[i] - a[i];
        z += (k / 2);
      }
    }
  }
  // cout<<c<<" "<<z<<endl;
  c *= 2;
  i = 0;
  while (z > c) {
    k = z - c;
    if (k & 1)
      k--;
    c += k;
    z += (k / 2);
  }
  while (c < z) {
    z++;
    c += 2;
  }

  if (c == z)
    cout << "Yes" << endl;
  else {
    // if(c>z)
    { cout << "No" << endl; }
    // else
    {}
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define read() freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
#define sf(n) scanf("%d", &n)
#define sfd(n) scanf("%lf", &n)
#define sff(n, m) scanf("%d%d", &n, &m)
#define sl(x) scanf("%I64d", &x)
#define sfl(n) scanf("%lld", &n)
#define sfc(n) scanf(" %c", &n)
#define sffl(n, m) scanf("%lld%lld", &n, &m)
#define sful(n) scanf("%ull", &n)
#define ll long long
#define ull unsigned long long int
#define endll pf("\n")
#define pf printf
#define pi acos(-1.0)
#define eps 1e-10
#define mem(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define xx first
#define yy second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define inf 1999999999
#define MP make_pair
#define MX 100005
#define mod 1000000007
#define var int i, j, k, n, m, z = 0, c = 0, t, x, y

/*
ll bigmod(ll x,ll y,ll m)
{
    if(y==0)return 1;
    ll n=bigmod(x,y/2,m);
    n=(n*n)%mod;
    if(y & 1)n=((n%m)*(x%m))%m;
    return n;
}
*/

int main() {
  ll var;
  cin >> n;
  ll int a[n + 1], b[n + 1];
  z = 0, c = 0, k = 0;
  for (i = 0; i < n; i++) {
    sfl(a[i]);
  }
  for (j = 0; j < n; j++)
    sfl(b[j]);

  for (i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      if (a[i] > b[i]) {
        k = a[i] - b[i];
        // cout<<c<<endl;
        if (z > 0) {
          if (k <= z) {
            z = z - k;
            b[i] += k;
          } else {
            b[i] += z;
            z = 0;
          }
          k = a[i] - b[i];
        }
        c += k;
        // cout<<k<<" "<<z<<endl;
      } else {
        k = b[i] - a[i];
        if (k & 1) {
          k++;
          c++;
          b[i] += 1;
        }
        if (c > 0) {
          if (k <= (c * 2)) {
            c = c - (k / 2);
            a[i] += k;
          } else {
            a[i] += (c * 2);
            c = 0;
          }
        }
        k = b[i] - a[i];
        z += (k / 2);
      }
    }
  }
  // cout<<c<<" "<<z<<endl;
  c *= 2;
  i = 0;
  while (z > c) {
    k = z - c;
    if (k & 1)
      k--;
    if (k == 0)
      k = 2;
    c += k;
    z += (k / 2);
  }
  while (c < z) {
    z++;
    c += 2;
  }

  if (c == z)
    cout << "Yes" << endl;
  else {
    // if(c>z)
    { cout << "No" << endl; }
    // else
    {}
  }
}