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;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  ll A[N];
  rep(i, N) cin >> A[i];

  ll s = 0;
  for (int i = 0; i < N; i++) {
    s += A[i];
  }

  ll sum = 0;
  for (int i = 0; i < N - 1; i++) {
    s -= A[i];
    sum += (A[i] * (s % 1000000007LL)) % 1000000007LL;
    sum = (sum % 1000000007LL);
  }
  cout << sum << 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;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  ll A[N];
  rep(i, N) cin >> A[i];

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

#include <iostream>
#include <string>

#define MODIFIER (1'000'000'007)

long long int add(long long int x, long long int y) {
  return ((x % MODIFIER) + (y % MODIFIER)) % MODIFIER;
}

long long int multiply(long long int x, long long int y) {
  return ((x % MODIFIER) * (y % MODIFIER)) % MODIFIER;
}

int main() {

  int N;
  std::cin >> N;

  int A[200'000];
  long long int sumASoFar = 0;
  long long int sum = 0;

  for (int i = 0; i < N; ++i) {
    int t;
    std::cin >> t;
    sum = add(sum, multiply(t, sumASoFar));
    sumASoFar += t;
  }

  std::cout << sum << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

#define MODIFIER (1'000'000'007)

long long int add(long long int x, long long int y) {
  return ((x % MODIFIER) + (y % MODIFIER)) % MODIFIER;
}

long long int multiply(long long int x, long long int y) {
  return ((x % MODIFIER) * (y % MODIFIER)) % MODIFIER;
}

int main() {

  int N;
  std::cin >> N;

  int A[200'000];
  long long int sum = 0;

  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];

    for (int j = 0; j < i; ++j) {
      long long int x = multiply(A[i], A[j]);
      sum = add(sum, x);
    }
  }

  std::cout << sum << std::endl;

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

#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
const ULL MOD = 1000000007;
#define rep(i, ns, ne) for (ULL i = ns; i < ne; ++i)

/*
int main() {
        int N;
        cin >> N;
        vector<int> A(N);
        vector<long long> B(N + 1, 0); //累積和テーブル
        int mod = 1000000007;

        for (int i = 0; i < N; i++)
        {
                cin >> A[i];
                B[i + 1] = B[i] + A[i];
        }

        long ans = 0;

        //i について全探索する
        for (int i = 0; i < N; i++)
        {
                //累積和を使ってA[i+1] + ... + A[N] を求める
                long long sum = (B[N] - B[i + 1]) % mod;

                ans += A[i] * sum;
                ans %= mod;
        }

        cout << ans << endl;
}*/

int main() {
  ULL N;
  cin >> N;
  vector<ULL> a(N);
  vector<ULL> b(N + 1, 0);
  rep(i, 0, N) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }

  ULL sum, ans = 0;
  rep(i, 0, N) {
    sum = (b[N] - b[i + 1]) % MOD;
    ans = (ans + a[i] * sum) % MOD;
  }

  cout << ans << endl;
  getchar();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
const ULL MOD = 1000000007;
#define rep(i, ns, ne) for (ULL i = ns; i < ne; ++i)

int main() {
  ULL N;
  cin >> N;
  vector<ULL> v(N);
  rep(i, 0, N) { cin >> v[i]; }

  ULL ans = 0;
  rep(i, 0, N - 1) {
    rep(j, i + 1, N) { ans += (v[i] * v[j]) % MOD; }
  }

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

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

int main() {
  ll mod = 1e9 + 7;

  ll n;
  cin >> n;
  vector<ll> a(n);
  ll s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  ll c = 0;
  for (int i = 0; i < n - 1; i++) {
    s -= a[i];
    c += ((s % mod) * (a[i] % mod)) % mod;
    c = c % mod;
  }

  cout << c;
}

Error: TLE

Fixed submission:

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

int main() {
  ll mod = 1e9 + 7;

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll s = 0;
  for (int i = 0; i < n - 1; i++) {

    for (int j = i + 1; j < n; j++)
      s = (s % mod + ((a[i] % mod) * (a[j] % mod)) % mod);
  }

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

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

int main() {
  int N;

  cin >> N;

  signed long long A[200000] = {};

  signed long long Ans = 0;
  signed long long kakekioku = 0;
  signed long long Anstmp = 0;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int i = N - 1; i >= 1; i--) {
    kakekioku += A[i + 1];
    Ans += (A[i] % 1000000007) * (kakekioku % 1000000007);
    Ans %= 1000000007;
  }
  cout << (Ans % 1000000007) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;

  cin >> N;

  signed long long A[200000] = {};

  signed long long Ans = 0;
  signed long long Anstmp = 0;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

  for (int i = 1; i <= (N - 1); i++) {
    for (int j = i + 1; j <= N; j++) {
      Ans += (A[i] % 1000000007) * (A[j] % 1000000007);
      Ans = Ans % 1000000007;
    }
  }
  cout << (Ans % 1000000007) << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
int main() {
  IOS;
  long long a, b, c, d, e, h, l, n, t, f, m, x, y, z,
      f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0,
      ma = 0, ma1 = 0, mi = 1000000000000000001;
  long long g, co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1,
               sum2 = 0, sum3 = 0, mu = 1, mu1 = 1;
  double pi = 2 * acos(0.0), dou;
  char ch, ch1, ch2;
  string str, str1, str2;
  cin >> n;
  m = 1000000000 + 7;
  vector<long long> v(n), v1(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v1[i] = v[i];
  }
  reverse(v1.begin(), v1.end());
  for (int i = 1; i < n; i++) {
    v1[i] += v1[i - 1];
  }
  reverse(v1.begin(), v1.end());
  for (int i = 0; i < n - 1; i++) {
    sum = sum + ((v[i] * (v1[i + 1] % m)) % m);
    sum = sum % m;
  }
  cout << sum % m << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n";
int main() {
  IOS;
  long long a, b, c, d, e, h, l, n, t, f, m, x, y, z,
      f1 = 0, flag = 0, flag1 = 0, co = 0, co1 = 0, co2 = 0, sum = 0, sum1 = 0,
      ma = 0, ma1 = 0, mi = 1000000000000000001;
  long long g, co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0, mul = 1,
               sum2 = 0, sum3 = 0, mu = 1, mu1 = 1;
  double pi = 2 * acos(0.0), dou;
  char ch, ch1, ch2;
  string str, str1, str2;
  cin >> n;
  m = 1000000000 + 7;
  vector<long long> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < n; i++) {
    if (v[i] == 0)
      continue;
    for (int j = i + 1; j < n; j++) {
      sum = sum + ((v[i] * v[j]) % m);
      sum = sum % m;
    }
  }
  cout << sum % m << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
// #include<string>
#include <math.h>
using namespace std;

int main() {
  long long int p = pow(10, 9) + 7;
  long long int N;
  cin >> N;
  long long int A[N];
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
    sum %= p;
  }

  long long int a = 0;
  for (int i = 0; i < N; i++) {
    sum -= A[i];
    if (sum < 0)
      sum += p;
    a += A[i] * sum;
    a %= p;
  }
  cout << a << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {
  long long int N;
  cin >> N;
  long long int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long int p = pow(10, 9) + 7;
  long long int a = 0;
  for (int j = 0; j < N; j++) {
    for (int i = 0; i < j; i++) {
      // cout << i << ":" << j<< endl;
      a += A[i] * A[j] % p;
      a %= p;
    }
  }
  cout << a << endl;

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

#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {

  long long n;
  cin >> n;
  long long arr[n];
  long long arr1[n];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i] %= mod;
  }
  arr1[0] = arr[0];
  for (int i = 1; i < n; i++) {
    arr1[i] = arr1[i - 1] + arr[i];
    arr1[i] %= mod;
  }
  for (int i = n - 1; i >= 1; i--) {
    sum += (arr[i] * arr1[i - 1]);
    sum %= mod;
  }

  cout << sum << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long mod = 1000000007;
  long long n;
  cin >> n;
  long long arr[n];
  long long sum = 0;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (arr[i] * arr[j]);
      sum %= mod;
    }
  }
  cout << sum << "\\n";
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  vector<ll> A(n);
  ll ans = 0;
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    sum = sum % mod;
  }
  for (ll i = 0; i < n - 1; i++) {
    sum = sum - A[i];
    if (sum < 0) {
      sum = mod + sum;
    }
    ans += A[i] * sum;
    ans = ans % mod;
  }
  cout << ans % mod << "\\n";
}


Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  vector<ll> A(n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n - 1; i++) {
    ll c = 0;
    for (int j = i + 1; j < n; j++) {
      c += A[j];
    }
    ans += A[i] * (c % mod) % mod;
  }
  cout << ans % mod << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

constexpr int64_t kMod = 1e9 + 7;
int64_t Modinv(int64_t a, int64_t m) {
  int64_t b = m, u = 1, v = 0;
  while (b) {
    int64_t t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  int64_t N;
  cin >> N;

  int64_t sum = 0;
  int64_t square_sum = 0;
  for (int64_t n = 0; n < N; ++n) {
    int64_t A;
    cin >> A;
    (sum += A) %= kMod;
    (square_sum += A * A) %= kMod;
  }
  int64_t sum_square = (sum * sum) % kMod;
  int64_t res = ((sum_square - square_sum) % kMod * Modinv(2, kMod)) % kMod;
  if (res < 0)
    res += kMod;
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

constexpr int64_t kMod = 1e9 + 7;

int main() {
  int64_t N;
  cin >> N;

  int64_t res = 0;
  vector<int64_t> nums;
  nums.reserve(N);
  for (int64_t n = 0; n < N; ++n) {
    int64_t A;
    cin >> A;
    for (const int64_t num : nums) {
      (res += (A * num)) %= kMod;
    }
    nums.push_back(A);
  }
  cout << res << endl;

  return 0;
}
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;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(200005);
  rep(i, N) cin >> A[i];
  vector<ll> S(200005);
  S[0] = A[0];
  for (int i = 0; i < N - 1; i++) {
    S[i + 1] = S[i] + A[i + 1];
  }
  ll sum = 0;
  ll mod = 1000000000 + 7;
  rep(i, N - 1) {
    ll a = (S[N - 1] - S[i]) % mod;
    sum += A[i] * a;
    sum %= mod;
  }
  cout << sum << 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;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(200005);
  rep(i, N) cin >> A[i];
  ll sum = 0;
  ll mod = 1000000000 + 7;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += A[i] * A[j] % mod;
      sum %= mod;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

long long int modinv(long long int a, long long int m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  long long int N;
  cin >> N;

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

  long long int ans = 0;
  long long int tmp1 = 0;
  long long int tmp2 = 0;

  for (long long int i = 0; i < N; ++i) {
    tmp1 += A[i];
    tmp2 += (A[i] * A[i]) % 1000000007;
    tmp1 %= 1000000007;
    tmp2 %= 1000000007;
  }
  tmp1 *= tmp1;
  tmp1 %= 1000000007;

  long long int tmp3 = 0;
  if (tmp1 - tmp2 < 0) {
    tmp3 = tmp1 - tmp2 + 1000000007;
  } else
    tmp3 = tmp1 - tmp2;

  ans = tmp3 * modinv(2, 1000000007);
  ans %= 1000000007;

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

int main() {
  long long int N;
  cin >> N;

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

  long long int ans = 0;
  for (long long int i = 0; i < N; ++i) {
    for (long long int j = i; j < N; ++j) {
      if (i < j) {
        ans += (A[i] * A[j]) % 1000000007;
      }
      ans %= 1000000007;
    }
  }

  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<int64_t> A(N);
  int64_t M = 1000000007LL;
  int64_t sum = 0;
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    sum += A.at(i);
    sum %= M;
  }
  for (int i = 0; i < N - 1; i++) {
    sum -= A.at(i);
    if (sum < 0)
      sum += M;
    ans += A.at(i) * sum;
    ans %= M;
  }
  cout << ans % M << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  int64_t M = 1000000007LL;
  int64_t sum = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += (A.at(i) % M) * (A.at(j) % M);
      sum %= M;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const int P = 1000000007;

int n;
int a[maxn];
long long sum[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    sum[i] = (sum[i - 1] + a[i]) % P;
  }
  long long ans = 0;
  for (int i = 2; i <= n; ++i) {
    ans = (ans + a[i] * sum[i - 1]) % P;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10009;
const int P = 1000000007;

int n;
int a[maxn];
long long sum[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    sum[i] = (sum[i - 1] + a[i]) % P;
  }
  long long ans = 0;
  for (int i = 2; i <= n; ++i) {
    ans = (ans + a[i] * sum[i - 1]) % P;
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define mp make_pair

#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

using namespace std;

int MOD = 1e9 + 7;

int main() {

  fastio();
  ll n, a[300000] = {0};
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= MOD;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= a[i];
    if (sum < 0)
      sum += MOD;
    ans += sum * a[i];
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define mp make_pair

#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define endl "\\n"

using namespace std;

int MOD = 1e9 + 7;

int main() {

  fastio();
  ll a, arr[300000] = {0};
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> arr[i];
  ll sum = 0;
  for (int i = 0; i < a; i++) {
    for (int j = i + 1; j < a; j++) {
      sum += arr[i] * arr[j];
      sum %= MOD;
    }
  }
  cout << sum << endl;

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

#include <iostream>

using namespace std;
long long MOD = 1e9 + 7;
int main() {
  long long n, sum = 0;
  cin >> n;
  long long Arr[n];
  long long pSa[n];
  for (long long i = 0; i < n; i++) {

    cin >> Arr[i];
  }
  for (long long j = 0; j < n; j++) {
    if (j == 0) {
      pSa[0] = Arr[0];
    } else {
      pSa[j] = pSa[j - 1] + Arr[j];
    }
  }
  for (long long k = 0; k < n; k++) {
    long long a = Arr[k] % MOD;
    long long b = (pSa[n - 1] - pSa[k]) % MOD;
    long long c = ((a % MOD) * (b % MOD)) % MOD;
    sum += c % MOD;
  }
  cout << sum % MOD << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, sum = 0;
  cin >> n;
  long long Arr[n];

  for (int i = 0; i < n; i++) {

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

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

#define Ff(i, a, b) for (i = a; i < b; i++)
#define Fb(i, a, b) for (i = a - 1; i >= b; i--)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define mp make_pair
#define PI 2 * asin(1)
#define all(x) x.begin(), x.end()
#define trav(x) for (auto &i : x)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define spc ' '

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<long long> vll;
typedef map<int, int> mi;
typedef map<long long, long long> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<long long, long long> umll;
typedef set<int> si;
typedef set<long long> sll;
typedef unordered_set<int> usi;
typedef unordered_set<long long> usll;

const ll N = 1e9 + 7;
#define int long long

void sub() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  int i, j;
  int s = 0;
  Ff(i, 0, n) cin >> a[i];
  int g = 0;
  Ff(i, 0, n) g += a[i];
  Ff(i, 0, n - 1) {
    s = (s + (a[i] % N) * ((g - a[i]) % N)) % N;
    g -= a[i];
  }
  cout << s << endl;
}

int32_t main() {
  FIO sub();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

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

#define Ff(i, a, b) for (i = a; i < b; i++)
#define Fb(i, a, b) for (i = a - 1; i >= b; i--)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define mp make_pair
#define PI 2 * asin(1)
#define all(x) x.begin(), x.end()
#define trav(x) for (auto &i : x)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define spc ' '

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<long long> vll;
typedef map<int, int> mi;
typedef map<long long, long long> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<long long, long long> umll;
typedef set<int> si;
typedef set<long long> sll;
typedef unordered_set<int> usi;
typedef unordered_set<long long> usll;

const ll N = 1e9 + 7;
#define int long long

void sub() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  int i, j;
  int s = 0;
  Ff(i, 0, n) cin >> a[i];
  Ff(i, 0, n - 1) {
    Ff(j, i + 1, n) { s = (s + ((a[i] % N) * (a[j] % N)) % N) % N; }
  }
  cout << s << endl;
}

int32_t main() {
  FIO sub();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  const long long int mod = 1e9 + 7;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  unsigned long long int suffix_sum = 0;
  unsigned long long int result = 0;
  unsigned long long int answer = 0;
  for (int j = n - 1; j >= 0; j--) {

    answer = v[j];
    suffix_sum %= mod;
    answer *= suffix_sum;
    suffix_sum += v[j];
    answer %= mod;
    result += answer;
  }
  cout << result % mod;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  const long long int mod = 1e9 + 7;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  unsigned long long int result = 0;
  unsigned long long int answer = 1;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      answer = v[i];
      answer %= mod;
      answer *= v[j];
      answer %= mod;
      result += answer;
    }
  }
  cout << result % mod;
}