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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
// now

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int Find(int x) { // 根の番号を探す
    if (d[x] < 0)
      return x;
    return d[x] = Find(d[x]);
  }
  bool unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (d[x] < d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { return Find(x) == Find(y); }
  int size(int x) { return (-d[Find(x)]); }
};

ll nCr(ll n, ll r) {
  ll ans = 1;
  for (ll i = n; i > n - r; i--) {
    ans *= i;
  }
  for (ll i = 1; i <= r; i++) {
    ans /= i;
  }
  return ans;
}

int nPr(int n, int r) {
  int ans = 1;
  for (int i = n; i > n - r; i--) {
    ans *= i;
  }
  return ans;
}
ll modd = 1e9 + 7;
ll wa(ll n, ll mi, ll ma) {
  ll ans = n * (mi + ma) / 2;
  return ans;
}
ll kw(ll k, ll n) {

  ll ans = 0;
  ll mi, ma;
  mi = wa(k, 0, k - 1);

  ma = wa(k, n - k + 1, n);

  return (ma - mi + 1) % modd;
}

int main() {
  ll n;
  ll d = 1e9 + 7;
  cin >> n;
  vector<ll> vec(n);
  ll sum = 0;

  REP(i, n) {
    cin >> vec[i];
    sum += vec[i];
    sum %= d;
  }

  ll ans = 0;
  REP(i, n) {
    sum -= vec[i];
    if (sum < 0)
      sum += d;
    ans += vec[i] * sum;
    ans %= d;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}

using namespace std;
// now

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int Find(int x) { // 根の番号を探す
    if (d[x] < 0)
      return x;
    return d[x] = Find(d[x]);
  }
  bool unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return false;
    if (d[x] < d[y]) {
      swap(x, y);
    }
    d[x] += d[y];
    d[y] = x;
    return true;
  }

  bool same(int x, int y) { return Find(x) == Find(y); }
  int size(int x) { return (-d[Find(x)]); }
};

ll nCr(ll n, ll r) {
  ll ans = 1;
  for (ll i = n; i > n - r; i--) {
    ans *= i;
  }
  for (ll i = 1; i <= r; i++) {
    ans /= i;
  }
  return ans;
}

int nPr(int n, int r) {
  int ans = 1;
  for (int i = n; i > n - r; i--) {
    ans *= i;
  }
  return ans;
}
ll modd = 1e9 + 7;
ll wa(ll n, ll mi, ll ma) {
  ll ans = n * (mi + ma) / 2;
  return ans;
}
ll kw(ll k, ll n) {

  ll ans = 0;
  ll mi, ma;
  mi = wa(k, 0, k - 1);

  ma = wa(k, n - k + 1, n);

  return (ma - mi + 1) % modd;
}

int main() {
  int n;
  cin >> n;

  vector<ll> vec(n);
  REP(i, n) { cin >> vec[i]; }
  ll ans = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (vec[i] * vec[j]) % (1000000000 + 7);
      ans %= (1000000000 + 7);
    }
  }
  cout << ans << endl;
}