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

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> sa(n - 1);
  ll x = 1000000000;
  rep(i, n) { cin >> a.at(i); }
  sa.at(0) = a.at(n - 1);
  rep(i, n - 2) { sa.at(i + 1) = (a.at(n - 2 - i) + sa.at(i)) % (x + 7); }
  reverse(sa.begin(), sa.end());
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += (a.at(i) * sa.at(i)) % (x + 7);
  }
  sum = sum % (x + 7);
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) { cin >> a.at(i); }
  ll sum = 0;
  ll x = 1000000000;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += (a.at(i) * a.at(j)) % (x + 7);
    }
  }
  sum = sum % (x + 7);
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

const int mod = 1'000'000'007;

struct mint {
  ll x; // typedef long long ll;
  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;
  }

  // for prime mod
  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; }
};

istream &operator>>(istream &is, mint &a) { return is >> a.x; }

ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  mint aSum;
  rep(i, n) { aSum += a.at(i); }
  mint aSum2 = aSum * aSum;
  mint bSum;
  rep(i, n) { bSum += a.at(i) * a.at(i) % mod; }
  mint res = (aSum2 - bSum) / 2;
  cout << res.x << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

const int mod = 1'000'000'007;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  ll res = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      res += a.at(i) * a.at(j) % mod;
      res %= mod;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
ll mod = 1e9 + 7;
ll a[2 * N];
void solve() {
  ll n, m = 0, k = 0, i = 1, j, p, f, x, y, z;
  ll ans = 0, sum = 0;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++) {
    ans += (a[i] * sum) % mod;
    sum += a[i];
    sum %= mod;
    ans %= mod;
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 100005;
ll mod = 1e9 + 7;
void solve() {
  ll n, m = 0, k = 0, i = 1, j, p, f, x, y, z;
  ll ans = 0, a[2 * N], b[N] = {};
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % mod;
    }
    ans %= mod;
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define boost ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int mod = 1e9 + 7;
/*
#define bigint __int128
__int128 abss(__int128 x){if(x>0)return x;
    return -x;
    }
*/
void solve() {
  int n;
  cin >> n;
  vector<ll> v(n);
  for (ll &i : v)
    cin >> i;
  ll ans = 0, sum = 0;
  for (int i = 0; i < n; i++)
    sum += v[i];

  for (int i = 0; i < n; i++) {
    sum -= v[i];
    ans = (ans % mod + ((v[i] % mod) * (sum % mod)) % mod) % mod;
  }
  cout << (ans % mod) << endl;
}
int main() {
  boost;
  int t;
  t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define boost ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int mod = 1e9 + 7;
/*
#define bigint __int128
__int128 abss(__int128 x){if(x>0)return x;
    return -x;
    }
*/
void solve() {
  int n;
  cin >> n;
  vector<ll> v(n);
  for (ll &i : v)
    cin >> i;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = (ans % mod + (v[i] * v[j]) % mod) % mod;
    }
  }
  cout << (ans % mod) << endl;
}
int main() {
  boost;
  int t;
  t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int MOD = 1000000007;
  int n;
  cin >> n;
  vector<long long> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec.at(i);

  vector<long long> rui(n + 1);
  for (int i = 0; i < n; i++)
    rui[i + 1] = rui[i] + vec[i];

  long long ans = 0;
  long sum;
  for (int i = 0; i < n; i++) {
    sum = (rui[n] - rui[i + 1]) % MOD;
    ans += vec[i] * sum;
    ans %= MOD;
  }

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


Error: TLE

Fixed submission:

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

int main() {
  int MOD = 1000000007;
  int n;
  cin >> n;
  vector<long long> vec(n);

  long long cnt = 0;
  for (int i = 0; i < n; i++)
    cin >> vec.at(i);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      cnt += (vec[i] % MOD * vec[j] % MOD);
      cnt %= MOD;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
int main() {
  LL n, ans = 0;
  cin >> n;
  vector<LL> a(n);
  for (LL i = 0; i < n; i++)
    cin >> a[i];
  LL subSum = 0;
  for (int i = 0; i < n - 1; i++) {
    subSum += a[i];
    subSum %= MOD;
    ans += subSum * a[i + 1];
    ans %= MOD;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
int main() {
  LL n, ans = 0;
  cin >> n;
  vector<LL> a(n);
  for (LL i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] % MOD) * (a[j] % MOD);
      ans %= MOD;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;
ll power(ll x, ll n) {
  if (x == 0)
    return 1;
  ll a = x, b = 1;
  while (n) {
    if (n & 1)
      b = ((b % mod) * (a % mod)) % mod;
    a = ((a % mod) * (a % mod)) % mod;
    n = n / 2;
  }
  return b % mod;
}
ll inv(ll a, ll b) { return (a * power(b, mod - 2) % mod) % mod; }
int main() {

  // freopen("testcase1.txt","r",stdin);
  // freopen("output1.txt","w",stdout);

  ll s1 = 0, s2 = 0, i, n, t;
  // cin>>t;
  t = 1;
  while (t--) {
    s1 = s2 = 0;
    cin >> n;
    ll a[n + 2];
    for (i = 0; i < n; i++)
      cin >> a[i];
    for (i = 0; i < n; i++)
      s1 = (s1 % mod + a[i] % mod) % mod;
    // cout<<s1<<endl;
    for (i = 0; i < n; i++)
      s2 = (s2 + ((a[i] % mod) * ((s1 - a[i]) % mod)) % mod + mod) % mod;
    cout << inv(s2, 2) % mod << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;
int main() {
  ll n, i, j, s = 0;

  cin >> n;
  ll a[n + 2];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++)
    for (j = i + 1; j < n; j++)
      s = (s + ((a[i] % mod) * (a[j] % mod)) % mod) % mod;
  cout << s;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

/*#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
*/

/*
ID: febrian3
LANG: C++11
PROB: task
*/

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr << #x << ": " << (x) << '\\n'
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll nax = 0;
ll n;

int main() {
  /*freopen("filename.in","r",stdin);
  freopen("filename.out","w",stdout);*/
  IOS cin >> n;
  vector<ll> arr(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  vector<ll> pref(n, 0);
  for (int i = n - 1; i >= 0; --i) {
    pref[i] = arr[i] % mod;
    if (i < n - 1)
      pref[i] = (pref[i] % mod + pref[i + 1] % mod) % mod;
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; ++i) {
    ll cur = ((arr[i] % mod) * (pref[i + 1] % mod)) % mod;
    ans = (ans + cur) % mod;
  }
  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr << #x << ": " << (x) << '\\n'
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll nax = 0;
ll n;

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

#include <bits/stdc++.h>
#define ll long long
#define OJ                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;
int mod = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  OJ;
  int n;
  cin >> n;
  int a[n];
  ll int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }
  ll int 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;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define OJ                                                                     \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
using namespace std;
int mod = 1000000007;

int main() {
  // OJ;
  ll ans = 0;
  int n, flag = 0;
  cin >> n;
  ll int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      flag = 1;
    }
  }
  if (flag) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % mod;
      ans %= mod;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

class solution {
public:
  long long pairProduct(vector<int> &A, int n) {
    unsigned long long sum = 0;
    for (int i = 0; i < n; ++i)
      sum += A[i];
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      sum -= A[i];
      ans = (ans + ((sum % MOD) * (A[i] % MOD)) % MOD) % MOD;
    }
    return ans;
  }
};

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  solution solve = solution();
  long long ans = solve.pairProduct(A, n);
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

class solution {
public:
  long long pairProduct(vector<int> &A, int n) {
    long long ans = 0;
    for (int i = 0; i < n - 1; ++i) {
      for (int j = i + 1; j < n; ++j) {
        ans += ((long long)A[i] * A[j]) % MOD;
        ans %= MOD;
      }
    }
    return ans;
  }
};

int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  solution solve = solution();
  long long ans = solve.pairProduct(A, n);
  cout << ans << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  ll mod = 7 + 1e9;
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) {
    cin >> a[i];
    if (i != 0)
      b[i] += b[i - 1] + a[i];
  }
  rep(i, n) {
    ll k = a[i] * ((b[n - 1] - b[i]) % mod) % mod;
    ans += k;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)

using namespace std;
using ll = long long;

int main() {
  ll mod = 7 + 1e9;
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) rep(j, i) {
    ans += (a[i] * a[j]) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<ll> b(n + 1, 0); // 累積和テーブル
  rep(i, n) {
    cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }

  ll ans = 0;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int mod = 1000000007;

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

  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j] % mod;
      ans %= mod;
    }
  }
  ans %= mod;
  cout << ans << endl;
  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 int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long int ans = 0LL;
  long long int MOD = int(pow(10, 9) + 7);
  long long int asum = accumulate(A.begin(), A.end(), 0LL);
  for (int i = 0; i < N - 1; i++) {
    asum -= A[i];
    ans += A[i] * (asum % MOD) % MOD;
    ans = ans % MOD;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  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;
  int MOD = int(pow(10, 9) + 7);
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j] % MOD;
    }
  }
  cout << ans % int(pow(10, 9) + 7) << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  const int64_t mod = 1e9 + 7;
  int64_t N;
  cin >> N;
  int64_t sum = 0;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
    sum += A.at(i);
    sum %= mod;
  }
  int64_t ans = 0;
  for (int64_t i = 0; i < N - 1; i++) {
    sum -= A.at(i);
    if (sum < 0) {
      sum += mod;
    }
    ans += (sum * A.at(i)) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  const int64_t mod = 1e9 + 7;
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += (A.at(i) * A.at(j)) % mod;
      sum %= mod;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mod = 1e9 + 7;

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> a(n);
    ll sum = 0, answer = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      answer += a[i] * sum;
      answer %= mod;
      sum += a[i];
      sum %= mod;
    }
    cout << answer;
  }
}
// 9.2 x 10^18

Error: TLE

Fixed submission:

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

const int mod = 1e9 + 7;

// ll multiply(ll x, ll y) {
// 	if (y > x) swap(x, y); //y is smaller
// 	ll answer = 0;
// 	if (x == 0 || y == 0) return 0;
// 	while (y--) {
// 		answer =  (answer+x)%mod;
// 	}
// 	return answer%mod;
// }

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ll answer = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        answer = (answer + (a[i] * a[j]) % mod) % mod;
      }
    }
    cout << answer;
    // cout<<multiply(1, 3);
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = (b); i < (n); i++)
using namespace std;
using ll = long long;
const ll M = 1000000007;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n + 10);
  vector<ll> s(n + 10);
  rep(i, n) {
    cin >> a[i];
    s[i + 1] += s[i] + a[i];
    s[i + 1] %= M;
  }
  rep(i, n - 1) {
    if (s[n] - s[i + 1] < 0)
      ans += a[i] * (s[n] - s[i + 1] + M);
    else
      ans += a[i] * (s[n] - s[i + 1]);
    ans %= M;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = (b); i < (n); i++)
using namespace std;
using ll = long long;
const ll M = 1000000007;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n + 10);
  rep(i, n) cin >> a[i];
  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % M;
      ans %= M;
    }
  cout << ans << endl;
}
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;

const int MOD = 1000000007;

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

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

  sort(a.begin(), a.end());

  vector<ll> sum(n + 1, 0);
  rep(i, n) sum[i + 1] = sum[i] + a[i];

  ll ans = 0;
  for (int i = 0; i < n - 1; i++)
    ans = (ans + (a[i] * ((sum[n] - sum[i + 1]) % MOD) % MOD)) % 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;
using ll = long long;

const int MOD = 1000000007;

ll binary_pow(ll n, ll p) {
  return p ? binary_pow(n * n % MOD, p >> 1) * (p & 1 ? n : 1) % MOD : 1;
}

ll combination(ll n, ll r) {
  ll x = 1, y = 1;
  r = min(r, n - r);
  rep(i, r) {
    x = x * (n - i) % MOD;
    y = y * (i + 1) % MOD;
  }
  return x * binary_pow(y, MOD - 2) % MOD;
}

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

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

  sort(a.begin(), a.end());
  vector<pair<int, int>> cnt(n);
  cnt[0] = make_pair(a[0], 1);

  int pairCnt = 1;
  for (int i = 1; i < n; i++)
    if (a[i - 1] == a[i])
      cnt[pairCnt - 1].second++;
    else
      cnt[++pairCnt - 1] = make_pair(a[i], 1);

  ll ans = 0;
  rep(i, pairCnt) if (cnt[i].second > 1) ans =
      (ans + combination(cnt[i].second, 2) * binary_pow(cnt[i].first, 2)) % MOD;

  rep(i, pairCnt - 1) for (int j = i + 1; j < pairCnt; j++) {
    ll temp1 = cnt[i].second * cnt[j].second % MOD;
    ll temp2 = cnt[i].first;
    temp2 = temp2 * cnt[j].first % MOD;
    ll temp3 = temp1 * temp2 % MOD;
    ans = (ans + temp3) % MOD;
  }

  while (ans < 0)
    ans += MOD;
  ans = ans % MOD;

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

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

#define int long long

const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> cum(n);
  cum[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--)
    cum[i] = (cum[i + 1] + a[i]) % MOD;

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += (a[i] * (cum[i + 1] % MOD)) % MOD;
  }

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


Error: TLE

Fixed submission:

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

#define int long long

const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % MOD;
    }
    ans %= MOD;
  }

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