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

#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tst                                                                    \\
  lli T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define lli long long int
#define vt vector
#define pb push_back
int main() {
  vite;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n;
  cin >> n;
  lli a[n];
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  lli sum = 0, p[n + 1] = {0};
  for (lli i = n - 1; i >= 0; i--)
    p[i] = (p[i + 1] + a[i]) % mod;
  for (lli i = 0; i < n; i++) {
    sum += a[i] * p[i + 1];
    sum %= mod;
  }
  cout << sum;
  cout << endl;
  // cout<< fixed << setprecision(6) << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define vite                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define tst                                                                    \\
  lli T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define lli long long int
#define vt vector
#define pb push_back
int main() {
  vite;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  lli n;
  cin >> n;
  lli a[n];
  for (lli i = 0; i < n; i++)
    cin >> a[i];
  lli sum = 0;
  for (lli i = 0; i < n; i++)
    for (lli j = i + 1; j < n; j++)
      sum = (sum + (a[i] * a[j]) % mod) % mod;

  cout << sum;
  cout << endl;
  // cout<< fixed << setprecision(6) << 1.0 * clock() / CLOCKS_PER_SEC;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

const long long D = 1000000007;

int main() {
  int N;
  long long q = 0;
  std::cin >> N;
  long long *A = new long long[N];
  for (int i = 0; i < N; i++)
    std::cin >> A[i];
  long long res = 0;
  for (int i = 0; i < N; i++)
    res += A[i];
  q += res / D;
  res %= D;
  res *= res;
  q += res / D;
  res %= D;
  // q = q*D;
  long long diagonal = 0;
  long long qd = 0;
  for (int i = 0; i < N; i++) {
    diagonal += (A[i] * A[i]);
    qd += diagonal / D;
    diagonal %= D;
  }
  res -= diagonal;
  if (res < 0) {
    res += D;
    q++;
  }
  // std::cout << q << std::endl;
  if ((q - qd) % 2 == 0)
    std::cout << res / 2 << std::endl;
  else
    std::cout << (D + res) / 2 << std::endl;
  delete[] A;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

const long long D = 1000000007;

int main() {
  int N;
  long long q = 0;
  std::cin >> N;
  long long *A = new long long[N];
  for (int i = 0; i < N; i++)
    std::cin >> A[i];
  long long res = 0;
  for (int i = 0; i < N; i++) {
    res += A[i];
  }
  q = res / D;
  res %= D;
  res *= res;
  q += res / D;
  res %= D;
  // q = q*D;
  for (int i = 0; i < N; i++) {
    res -= (A[i] * A[i]);
    while (res < 0) {
      res += D;
      q++;
    }
  }
  // std::cout << q << std::endl;
  if (q % 2 == 0)
    std::cout << res / 2 << std::endl;
  else
    std::cout << (D + res) / 2 << std::endl;
  delete[] A;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
// int a[100010],n,j;
int main() {
  int size;
  cin >> size;
  long long a[size];
  for (int i = 0; i < size; i++)
    cin >> a[i];
  long long ans = 0;
  long long suffix[size];
  long long sum = a[size - 1];
  suffix[size - 1] = a[size - 1];
  for (int i = size - 2; i >= 1; i--) {
    sum += a[i];
    sum %= mod;
    suffix[i] = sum;
  }
  for (int i = 0; i < size - 1; i++) {
    ans += (a[i] % mod) * (suffix[i + 1] % mod) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  // for(int i=1;i<size;i++)
  //   cout << suffix[i] << " " ;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

long long modInverse(long long a, long long m) {
  long long m0 = m;
  long long y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    // q is quotient
    long long q = a / m;
    long long t = m;

    // m is remainder now, process same as
    // Euclid's algo
    m = a % m, a = t;
    t = y;

    // Update y and x
    y = x - q * y;
    x = t;
  }

  // Make x positive
  if (x < 0)
    x += m0;

  return x;
}

int main() {
  fast_cin;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  const ll M = 1e9 + 7;
  ll N;
  cin >> N;
  vector<ll> A(N);

  ll tot_sum = 0, sq_sum = 0;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    tot_sum = (tot_sum + A[i]) % M;
    ll temp = (A[i] * A[i]) % M;
    sq_sum = (sq_sum + temp) % M;
  }
  tot_sum = (tot_sum * tot_sum) % M;
  ll ans = (tot_sum - sq_sum + M) % M;

  ll inv = modInverse(2, M);
  ans = (ans * inv) % M;

  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

int main() {
  fast_cin;
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  const ll M = 1e9 + 7;
  ll N;
  cin >> N;
  vector<ll> A(N);

  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll curr = 0;

  for (ll i = 0; i < N - 1; i++) {
    for (ll j = i + 1; j < N; j++) {
      ll temp = ((A[i] % M) * (A[j] % M)) % M;
      curr = (curr + temp) % M;
    }
  }
  cout << curr << "\\n";
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int N = 2e5 + 7;
const int mod = 1e9 + 7;
long long arr[N];
long long num[N];
int main() {
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
  }
  for (int i = n; i >= 1; i--) {
    arr[i] = (arr[i + 1] + num[i]) % mod;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = (ans + num[i] * arr[i + 1]) % mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int mod = 1e9 + 7;

long long num[200010];

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n; i++)
    scanf("%lld", &num[i]);
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      ans = (ans + num[i] * num[j]) % mod;
  cout << ans;
  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++)
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;

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

  ll ans = 0, sum = 0;
  rep(i, n) sum += a[i];
  rep(i, n - 1) {
    sum -= a[i];
    sum = (sum % MOD + MOD) % MOD;
    ans = (ans + a[i] * sum) % 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++)
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;

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

  ll ans = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      ans = (ans + a[i] * a[j]) % 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 mod = 1000000007;
  int N;
  cin >> N;
  long long A[N];
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
    sum %= mod;
  }
  long ans = 0;

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

Error: TLE

Fixed submission:

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

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

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

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

int main() {
  int N;
  cin >> N;
  vector<long> A(N);
  long mod = 1e+9 + 7;

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


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<long> A(N);
  long mod = 1e+9 + 7;

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

#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
#define MOD 1000000007

ll calc(ll a, ll b) {
  ll ma = a % MOD, mb = b % MOD;
  return (ma * mb) % MOD;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<ll> a(n), b(n + 1, 0);
  rep(i, n) {
    std::cin >> a[i];
    b[i + 1] = b[i] + a[i];
  }

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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
#define MOD 1000000007

ll calc(ll a, ll b) {
  ll ma = a % MOD, mb = b % MOD;
  return (ma * mb) % MOD;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<ll> a(n);
  rep(i, n) std::cin >> a[i];

  ll ans = 0;
  rep(i, n) {
    for (int j = i + 1; j < n; ++j) {
      ans += calc(a[i], a[j]);
      ans %= MOD;
    }
  }
  std::cout << ans << std::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++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int n;
  cin >> n;
  vector<int> V(n);
  rep(i, n) { cin >> V[i]; }

  long long ans = 0;

  ll sum = 0;
  for (int j = 0; j < n; j++) {
    sum += V[j];
  }

  for (int i = 0; i < n; i++) {
    ll sum2;
    sum -= V[i];
    sum2 = sum;
    sum2 %= 1000000007;
    sum2 *= V[i];
    ans += sum2;
    ans %= 1000000007;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int n;
  cin >> n;
  vector<int> V(n);
  rep(i, n) { cin >> V[i]; }

  long long ans = 0;

  for (int i = 0; i < n; i++) {
    ll sum = 0;
    for (int j = i + 1; j < n; j++) {
      sum += V[j];
    }
    sum %= 1000000007;
    sum *= V[i];
    ans += sum;
    ans %= 1000000007;
  }

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


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, n, v) for (i = n; i < v; i++)
#define per(i, n, v) for (i = n; i > v; i--)
#define ff first
#define ss second
#define pp pair<ll, ll>
#define ll long long
#define endl '\\n'
void solve() {
  ll n, a, m = 0, b = 1, c, k = 0, i, j, l = 1e9 + 7;
  string s, r, y;
  cin >> n;
  ll ar[n];
  rep(i, 0, n) {
    cin >> ar[i];
    k += ar[i];
    k %= l;
  }
  rep(i, 0, n) {
    k -= ar[i];
    if (k < 0)
      k += l;
    m += (ar[i] * k);
    m %= l;
  }
  cout << m << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, n, v) for (i = n; i < v; i++)
#define per(i, n, v) for (i = n; i > v; i--)
#define ff first
#define ss second
#define pp pair<ll, ll>
#define ll long long
#define endl '\\n'
void solve() {
  ll n, a, m = 0, b = 1, c, k = 0, i, j, l = 1e9 + 7;
  string s, r, y;
  cin >> n;
  ll ar[n];
  rep(i, 0, n) cin >> ar[i];
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      k += (ar[i] * ar[j]);
      k %= l;
    }
  }
  cout << k % l;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll 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 rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;

const ll mod = 1e9 + 7;
int main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  ll temp = 0;
  rep2(i, 1, N) temp += A[i];
  temp %= mod;
  ans += temp * A[0];
  ans %= mod;
  rep2(i, 1, N - 1) {
    temp -= A[i];
    if (temp < 0)
      temp += mod;
    ans += (A[i] * temp) % mod;
    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 rep2(i, a, n) for (int i = (a); i < (n); i++)
#define all(vec) vec.begin(), vec.end()
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
int main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  rep(i, N - 1) {
    rep2(j, i + 1, N) {
      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;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const ll MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N), sum(N + 1);
  rep(i, N) {
    int a;
    scanf("%d", &a);
    A[i] = a;
  }
  ll s = 0;
  for (int i = N - 1; i >= 0; i--) {
    sum[i] = sum[i + 1] + A[i];
    sum[i] %= MOD;
  }

  ll res = 0;
  rep(i, N) {
    res += A[i] * sum[i + 1];
    res %= MOD;
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
const ll INF = (1LL << 60);
const ll MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) {
    int a;
    scanf("%d", &a);
    A[i] = a;
  }

  ll res = 0;
  rep(i, N) {
    ll tmp = 0;
    for (int j = i + 1; j < N; j++) {
      tmp += A[j];
    }
    tmp %= MOD;
    res += A[i] * tmp;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

#define M 1000000007

using namespace std;

int main() {
  int n;
  vector<long long> a;
  long long fact = 0;
  long long sum = 0;

  cin >> n;

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

    a.push_back(next);
    fact = (fact + next) % M;
  }

  for (int i = 0; i < n; i++) {
    fact = (fact - a[i] + M) % M;
    sum = (sum + (a[i] * fact) % M) % M;
  }

  cout << sum % M << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define M 1000000007

using namespace std;

int main() {
  int n;
  vector<long long> a;
  long long sum = 0;

  cin >> n;
  vector<long long> sum_vec = vector<long long>(n, 0);

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

    a.push_back(next % M);

    for (int j = 0; j < i; j++) {
      sum_vec[j] = (sum_vec[j] + a[i]) % M;
    }
  }

  for (int i = 0; i < n; i++) {
    sum = sum + (a[i] * sum_vec[i]) % M;
  }

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

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long mod = 1000000007;
  int n;
  int ans = 0;
  cin >> n;
  vector<long> a(n);
  vector<long> add(n);
  // long addtmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  add.at(n - 1) = a.at(n - 1);
  // cout << n - 1 << " " << add.at(n - 1) << endl;
  for (int i = n - 2; i >= 0; i--) {
    add.at(i) = ((long)(add.at(i + 1) + a.at(i))) % mod;
    // cout << "add  i = " << i << " " << add.at(i) << endl;
  }
  for (int i = 0; i < n - 1; i++) {
    long mul = (long)(a.at(i) * add.at(i + 1));
    int tmp = mul % mod;
    ans = ((long)(ans + tmp)) % mod;
    // cout << "ans = " << ans << endl;
    // for(int j = i + 1; j < n; j++){
    //     long mul = (long)(a.at(i) * a.at(j));
    //     int tmp = mul % mod;
    //     ans = ((long)(ans + tmp)) % mod;
    // }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long mod = 1000000007;
  int n;
  int ans = 0;
  cin >> n;
  vector<long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    // cout << "a.at = " << a.at(i) << endl;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long mul = (long)(a.at(i) * a.at(j));
      // cout << "at" << a.at(i) << " " << a.at(j) << endl;
      int tmp = mul % mod;
      // cout << "i j = " << i << " " << j << " " << mul << endl;
      // cout << "tmp = " << tmp << endl;
      ans = ((long)(ans + tmp)) % mod;
      // cout << "ans = " << ans << endl;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

const long long int MOD = 1000000007;

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

  std::vector<long long int> a(n);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    sum += a[i];
    sum %= MOD;
  }

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

  std::cout << ans << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

const int MOD = 1000000007;

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

  std::vector<unsigned long long int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

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

  std::cout << ans << std::endl;

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

#include <bits/stdc++.h>
using namespace std;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pi pair<int, int>
#define F first
#define S second
#define PB push_back
#define EPB emplace_back
#define MP make_pair
#define deb(x) cerr << #x << ":" << x << endl;
#define deba(x)                                                                \\
  cerr << #x << endl;                                                          \\
  for (auto a : x)                                                             \\
    cerr << a << " ";                                                          \\
  cerr << endl;
const int MOD = 1e9 + 7;
const double EPS = 0.01;

int main() {
  fastio();
  int n;
  cin >> n;
  ll a[n];
  ll suf[n];
  suf[n - 1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  for (int i = n - 2; i >= 0; i--) {
    suf[i] = (suf[i + 1] % MOD + a[i + 1] % MOD) % MOD;
    sum = (sum % MOD + ((a[i] % MOD) * (suf[i] % MOD)) % MOD) % MOD;
  }
  cout << sum << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pi pair<int, int>
#define F first
#define S second
#define PB push_back
#define EPB emplace_back
#define MP make_pair
#define deb(x) cerr << #x << ":" << x << endl;
#define deba(x)                                                                \\
  cerr << #x << endl;                                                          \\
  for (auto a : x)                                                             \\
    cerr << a << " ";                                                          \\
  cerr << endl;
const int MOD = 1e9 + 7;
const double EPS = 0.01;

int main() {
  fastio();
  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = (sum % MOD + ((a[i] % MOD) * (a[j] % MOD)) % MOD) % MOD;
    }
  }
  cout << sum << '\\n';

  return 0;
}