alexjercan/bugnet huggingface dataset:
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  vector<long long> A(N + 1), sum(N + 1);
  long long mod = 1000000007;
  sum.at(0) = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
    sum.at(i) = sum.at(i - 1) + A.at(i);
  }
  long long ans = 0;
  for (int i = 1; i < N; i++) {
    ans += ((A.at(i) % mod) * ((sum.at(N) - sum.at(i)) % mod)) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) cin >> A.at(i);
  long long ans = 0;
  rep(i, N) {
    for (int j = i + 1; j < N; j++) {
      ans += ((A.at(i) % 1000000007) * (A.at(j) % 1000000007)) % 1000000007;
      ans %= 1000000007;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;

const int M = (int)2e5;
const int N = (int)5e5;
const int inf = 0x3f3f3f3f;
const ll mod = (ll)1e9 + 7;
const double eps = 1e-6;

int n, a[M + 5];

int main() {
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  ll sum = 0, ans = 0;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), sum = (sum + a[i]) % mod;
  for (int i = 1; i <= n; ++i) {
    sum = (sum - a[i]) % mod;
    ans = (ans + sum * a[i] % mod) % mod;
  }
  ans = (ans % mod + mod) % mod;
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;

const int M = (int)1e5;
const int N = (int)5e5;
const int inf = 0x3f3f3f3f;
const ll mod = (ll)1e9 + 7;
const double eps = 1e-6;

int n, a[M + 5];

int main() {
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  ll sum = 0, ans = 0;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), sum = (sum + a[i]) % mod;
  for (int i = 1; i <= n; ++i) {
    sum = (sum - a[i]) % mod;
    ans = (ans + sum * a[i] % mod) % mod;
  }
  ans = (ans % mod + mod) % mod;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <string.h>

#include <algorithm>
#include <iostream>

#define rep1(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main(void) {
  int N;
  cin >> N;
  int A[N];
  rep1(i, N) cin >> A[i];
  ll B[N]; // 累積和
  B[0] = A[0];
  rep2(i, 1, N) B[i] = B[i - 1] + A[i];

  ll ans = 0;
  ll sum;

  rep1(i, N - 1) {
    sum = (B[N - 1] - B[i]) % MOD;
    ans += (ll)A[i] * sum;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <string.h>

#include <algorithm>
#include <iostream>

#define rep1(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define MOD 1000000007

using namespace std;
using ll = long long;

int main(void) {
  int N;
  cin >> N;
  int A[10];
  rep1(i, N) cin >> A[i];
  ll B[10]; // 累積和
  B[0] = A[0];
  rep2(i, 1, N) B[i] = B[i - 1] + A[i];

  ll sum = 0;

  rep1(i, N - 1) {
    sum += (ll)A[i] * (B[N - 1] - B[i]);
    sum %= MOD;
  }

  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {

  long long asum = 0, sum = 0, N, i;
  cin >> N;
  vector<long long> A(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
    asum += A[i];
  }
  for (i = 0; i < N; i++) {

    asum -= A[i];
    sum += (A[i] * (asum % 1000000007)) % 1000000007;
  }
  cout << sum % 1000000007 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  long long asum = 0, sum = 0, N, i, j;
  cin >> N;
  vector<long long> A(N);
  for (i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (i = 0; i < N; i++) {
    for (j = i + 1; j < N; j++) {
      sum += (A[i] * A[j]) % 1000000007;
    }
  }
  cout << sum % 1000000007 << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

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

  ll a[n], b[n + 1];
  b[0] = 0;
  ll mod = 1000000007;
  rep(i, n) {
    cin >> a[i];
    b[i + 1] = a[i] + b[i];
  }

  ll ans = 0;
  rep(i, n) {
    ll sum = (b[n] - b[i + 1]) % mod;
    ans += a[i] * sum;
    ans %= mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

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

  ll a[n];
  rep(i, n) cin >> a[i];

  ll ans = 0;
  ll mod = 1000000007;
  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      ans += ((a[i] % mod) * (a[j] % mod)) % mod;
    }
  }

  ans %= mod;
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define m 1000000007
using namespace std;
const int mmax = 200005;
ll n;
ll a[mmax];
ll f[mmax][3];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("test.inp", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i][1] = (f[i - 1][1] + a[i]) % m;
  }
  for (int i = 1; i <= n; i++) {
    f[i][2] = ((f[i - 1][1] * a[i]) % m + f[i - 1][2]) % m;
  }
  cout << f[n][2] % m;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define m 1000000007
using namespace std;

ll n;
ll a[100001];
ll f[100001][3];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("test.inp", "r", stdin);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    f[i][1] = (f[i - 1][1] + a[i]) % m;
  }
  for (int i = 1; i <= n; i++) {
    f[i][2] = ((f[i - 1][1] * a[i]) % m + f[i - 1][2]) % m;
  }
  cout << f[n][2] % m;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
long long n, a[N], s[N];
long long res = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
    s[i] %= MOD;
  }
  for (int i = 1; i <= n; i++) {
    res += 1LL * a[i] * (s[n] - s[i] + MOD) % MOD;
    res %= MOD;
  }
  printf("%lld\\n", res);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 20005;
const int MOD = 1e9 + 7;
long long n, a[N], s[N];
long long res = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  for (int i = 1; i <= n; i++) {
    res += 1LL * a[i] * (s[n] - s[i]);
    res %= MOD;
  }
  printf("%lld\\n", res);
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
  bool operator==(const mint a) const { return x == a.x; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int n;
mint a[222222];

int main() {
  while (cin >> n) {
    mint sum = 0, x = 0;
    for (int i = 0; i < n; ++i) {
      ll t;
      cin >> t;
      a[i] = t;
      sum += a[i];
    }
    for (int i = 0; i < n; ++i) {
      x += a[i] * (sum - a[i]);
    }
    cout << x / 2 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
  bool operator==(const mint a) const { return x == a.x; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int n;
mint a[111111];

int main() {
  while (cin >> n) {
    mint sum = 0, x = 0;
    for (int i = 0; i < n; ++i) {
      ll t;
      cin >> t;
      a[i] = t;
      sum += a[i];
    }
    for (int i = 0; i < n; ++i) {
      x += a[i] * (sum - a[i]);
    }
    cout << x / 2 << endl;
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  long long N;
  std::cin >> N;
  std::vector<long long> A(N);
  std::vector<long long> S(N + 1);
  // [a_0, a_1, a_2, a_3]
  // a_0*a_1 + a_0*a_2 + a_0*a_3 + a_1*a_2 + a_1*a_3 + a_2*a_3
  // = a_0 * (a_1 + a_2 + a_3) + a_1 * (a_2 + a_3) + a_2 * a_3
  S[0] = 0;
  for (long long i = 0; i < N; i++) {
    std::cin >> A[i];

    S[i + 1] = (S[i] + A[i]) % 1000000007;
  }
  // S_0 = 0
  // S_1 = a_0
  // S_2 = a_0 + a_1
  // S_2 - S_0 = a_0 + a_1
  // S_j - S_i = a_i + a_(i+1) + ... + a_(j-1)
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    // a_i * (a_(i+1) + a_(i+2) + ... + a_(N-1))
    long long tmp = S[N] - S[i + 1];
    if (tmp < 0)
      tmp += 1000000007;
    tmp = (A[i] * tmp) % 1000000007;
    ans = (ans + tmp) % 1000000007;
  }
  std::cout << ans << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  long long N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (auto &&e : A) {
    std::cin >> e;
  }
  long long ans = 0;
  for (long long i = 0; i < N - 1; i++) {
    for (long long j = i + 1; j < N; j++) {
      ans = (ans + A[i] * A[j]) % 1000000007;
    }
  }
  std::cout << ans << std::endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9 + 7;
ll a[200100];
ll sum = 0;
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    sum += a[i];
    sum %= MOD;
  }
  ll ans = 0;
  rep(i, n) {
    sum -= a[i];
    if (sum < 0)
      sum += MOD;
    ans += a[i] * sum % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9 + 7;
ll a[100100];
int main() {
  int n;
  cin >> n;
  __int128_t sum = 0;
  __int128_t squaresum = 0;
  rep(i, n) { cin >> a[i]; }
  rep(i, n) {
    sum += a[i];
    // sum %= MOD;
    squaresum += (a[i] * a[i]);
    // squaresum %= MOD;
  }

  // ll f = sum * sum;
  __int128_t f = sum * sum;
  __int128_t ans = (f - squaresum) / 2;

  ans %= MOD;
  int x = ans;
  printf("%d", x);
  // cout << ans << endl;
  cout << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;
long long a[200005];
long long sum[200005];
const int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  long long num = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--) {
    sum[n - i + 1] = sum[n - i] + a[i];
    sum[n - i + 1] %= mod;
  }
  for (int i = 1; i < n; i++) {
    num += a[i] * sum[n - i] % mod;
    num %= mod;
  }
  cout << num % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;
long long a[200005];
const int mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  long long num = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      num += a[i] * a[j] % mod;
      num %= mod;
    }
  }
  cout << num << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b, c) for (ll i = a; i < b; i += c)
#define r(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define mod 1000000007
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll n;
  cin >> n;
  ll ar[n];
  ll sum = 0;
  f(i, 0, n, 1) { cin >> ar[i]; }
  ll in = ar[n - 1];
  r(i, n - 1, 1, 1) {
    // cout << in << "\\n";
    sum += (ar[i - 1] * in);
    in += ar[i - 1];
    sum %= mod;
    in %= mod;
    // cout << ar[i-1] << " " << sum  << " " << in << "\\n";
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b, c) for (ll i = a; i < b; i += c)
#define r(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define mod 1000000007
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll n;
  cin >> n;
  ll ar[n];
  ll sum = 0;
  f(i, 0, n, 1) { cin >> ar[i]; }
  f(i, 0, n - 1, 1) {
    f(j, i + 1, n, 1) {
      sum += (ar[i] * ar[j]);
      sum %= mod;
    }
  }
  cout << sum;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const long long MOD = 1e9 + 7;
typedef long long ll;

inline void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

ll findProductSum(ll A[], ll n) {
  ll array_sum = 0;
  for (ll i = 0; i < n; i++)
    array_sum = modAdd((array_sum), A[i]);

  ll array_sum_square = modMul((array_sum), (array_sum));

  ll individual_square_sum = 0;
  for (ll i = 0; i < n; i++)
    individual_square_sum =
        modAdd((individual_square_sum), modMul((A[i]), (A[i])));

  return (modDiv(modSub(array_sum_square, individual_square_sum), 2));
}

int main() {
  ll n;
  cin >> n;
  ll A[n + 2];

  for (ll i = 0; i < n; i++)
    cin >> A[i];
  ll ans = findProductSum(A, n);

  cout << ans % MOD << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const long long MOD = 1e9 + 7;
typedef long long ll;

inline void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int main() {

  ll n;
  cin >> n;
  ll a[n + 2];

  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll s = 0;
    for (ll j = 0; j < i; j++) {
      s = modAdd(a[j], s);
    }
    ans = modAdd(ans, modMul(s, a[i]));
  }

  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  long long n;
  const unsigned int m = 1000000007;
  cin >> n;
  ll sum = 0, ans = 0;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] = arr[i];
    sum = sum + arr[i];
  }
  for (ll i = 0; i < n; i++) {
    int s = (sum - arr[i]) % m;
    ans = (ans + (s * arr[i])) % m;
    sum = sum - arr[i];
  }
  cout << ans % m;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  long long n;
  const unsigned int m = 1000000007;
  cin >> n;
  ll sum = 0;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] = arr[i] % m;
  }

  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      sum = (sum % m + (arr[i] * arr[j]) % m) % m;
    }
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  IOS;
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll ans = 0;
  sort(ALL(a));

  vector<ll> s(n + 1);
  REP(i, n) { s[i + 1] = (a[i] + s[i]); }
  for (ll i = 0; i < n - 1; i++) {
    ll v = (s[n] - s[i + 1]) % MOD;
    ans += (a[i] * v) % MOD;
    // cout << a[i] << " x " << s[n] - s[i+1] << endl;
    ans %= MOD;
  }

  COUT(ans);

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

vector<ll> a(20010);
int n;

int main() {
  IOS;
  cin >> n;
  REP(i, n) cin >> a[i];
  ll ans = 0;

  vector<ll> s(n + 1);
  REP(i, n) { s[i + 1] = a[i] + s[i]; }
  for (int i = 0; i < n - 1; i++) {
    ans += (a[i] * (s[n] - s[i + 1])) % MOD;
    ans %= MOD;
  }

  COUT(ans);

  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  long long sum = 0;
  for (int i = 0; i < n; i++)
    sum += a.at(i);
  for (int i = 0; i < n - 1; i++) {
    sum -= a.at(i);
    ans += (a.at(i) * (sum % 1000000007)) % 1000000007;
  }
  cout << ans % 1000000007 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a.at(i);

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a.at(i) * a.at(j)) % 1000000007;
    }
  }
  cout << ans % 1000000007 << endl;
}