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

#include <cstring>
#include <iostream>
using namespace std;

int xx[1000], yy[1000];
int wf[1000][1000];
int main() {
  int n, X, Y;
  cin >> n;
  while (cin >> n >> X >> Y && n) {
    memset(wf, 0, sizeof(wf));
    int sx, sy = 0;
    cin >> sx >> sy;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      xx[i] = x, yy[i] = y;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i != j && (xx[i] - xx[j]) * (xx[i] - xx[j]) +
                              (yy[i] - yy[j]) * (yy[i] - yy[j]) <=
                          2500)
          wf[i][j] = 1;
    int p[1000];
    int q[1000];
    for (int i = 0; i < 1000; i++)
      p[i] = -1, q[i] = -1;
    for (int i = 0; i < n; i++) {
      if ((sx - xx[i]) * (sx - xx[i]) + (sy - yy[i]) * (sy - yy[i]) <= 100) {
        p[i] = X;
      }
    }
    bool flag;
    int cnt = 0;
    while (cnt != 200000) {
      flag = false; /*
       for(int j = 0 ; j < n ; j++)
               cout << p[j] << " " << q[j] << endl;
       cout << endl;*/
      for (int i = 0; i < n; i++) {
        if (p[i] > 0) {
          flag = true;
        }
        p[i]--;
        q[i]--;
      }
      for (int i = 0; i < n; i++) {
        if (p[i] == 0) {
          q[i] = Y;
        }
      }
      for (int i = 0; i < n; i++) {
        if (p[i] == 0) {
          for (int j = 0; j < n; j++) {
            if (wf[i][j] && q[j] < 0) {
              p[j] = X;
              flag = true;
            }
          }
        }
      }
      if (!flag)
        break;
      cnt++;
    }
    if (cnt == 200000)
      cout << "You're always welcome!" << endl;
    else
      cout << cnt << endl;
  }
}
/*
11011

*/

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

int xx[1000], yy[1000];
int wf[1000][1000];
int main() {
  int n, X, Y;
  cin >> n;
  while (cin >> n >> X >> Y && n) {
    memset(wf, 0, sizeof(wf));
    int sx, sy = 0;
    cin >> sx >> sy;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      xx[i] = x, yy[i] = y;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (i != j && (xx[i] - xx[j]) * (xx[i] - xx[j]) +
                              (yy[i] - yy[j]) * (yy[i] - yy[j]) <=
                          2500)
          wf[i][j] = 1;
    int p[1000];
    int q[1000];
    for (int i = 0; i < 1000; i++)
      p[i] = -1, q[i] = -1;
    for (int i = 0; i < n; i++) {
      if ((sx - xx[i]) * (sx - xx[i]) + (sy - yy[i]) * (sy - yy[i]) <= 100) {
        p[i] = X;
      }
    }
    bool flag;
    int cnt = 0;
    while (cnt != 400000) {
      flag = false; /*
       for(int j = 0 ; j < n ; j++)
               cout << p[j] << " " << q[j] << endl;
       cout << endl;*/
      for (int i = 0; i < n; i++) {
        if (p[i] > 0) {
          flag = true;
        }
        p[i]--;
        q[i]--;
      }
      for (int i = 0; i < n; i++) {
        if (p[i] == 0) {
          q[i] = Y;
        }
      }
      for (int i = 0; i < n; i++) {
        if (p[i] == 0) {
          for (int j = 0; j < n; j++) {
            if (wf[i][j] && q[j] <= 0) {
              p[j] = X;
              flag = true;
            }
          }
        }
      }
      if (!flag)
        break;
      cnt++;
    }
    if (cnt == 400000)
      cout << "You're always welcome!" << endl;
    else
      cout << cnt << endl;
  }
}
/*
11011

*/