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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> sum(N);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (ll i = N - 2; i >= 0; i--) {
    sum[i] = (sum[i + 1] + A[i + 1]) % DIV;
    // cerr << sum[i] << endl;
  }

  ll total = 0;
  for (size_t i = 0; i < N - 1; i++) {
    // sum -= A[i];
    total += (A[i] * sum[i]) % DIV;
    total %= DIV;
    /*
    for(size_t j=i+1;j<N; j++)
    {
        ll mul = (A[i]*A[j]) % DIV;
        sum += mul;
        sum %= DIV;
    }
    */
  }
  cout << total << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18 + 1;
const ll DIV = 1000000007;
// #define TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef TEST
  chrono::system_clock::time_point start, end;
  start = chrono::system_clock::now();
#endif
  long N;
  cin >> N;
  vector<ll> A(N);
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll sum = 0;
  for (size_t i = 0; i < N - 1; i++) {
    for (size_t j = i + 1; j < N; j++) {
      ll mul = (A[i] * A[j]) % DIV;
      sum += mul;
      sum %= DIV;
    }
  }
  cout << sum << endl;
#ifdef TEST
  end = chrono::system_clock::now();
  cerr << static_cast<double>(
              chrono::duration_cast<chrono::microseconds>(end - start).count() /
              1000.0)
       << "[ms]" << endl;
#endif
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, sum = 0;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a.at(i);
    sum += a.at(i);
    sum %= 1000000007;
  }

  ll t = 0;
  for (ll i = 0; i < n - 1; i++) {
    sum -= a.at(i);
    if (sum < 0)
      sum += 1000000007;

    t += a.at(i) * sum;
    t %= 1000000007;
  }

  cout << t << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  ll t = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; j++) {
      t += a.at(i) * a.at(j);
      t %= 1000000007;
    }
  }

  cout << t << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define frfr                                                                   \\
  freopen("input.text", "r", stdin); // freopen("output.txt","w",stdout)
#define LL long long int
#define wh                                                                     \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define foro(i, n) for (int i = 0; i < n; i++)
#define forn(i, k, n) for (int i = k; i < n; i++)
#define PB push_back
#define mod 1000000007
#define UMP unordered_map<int, int>
int main() {
  LL n;
  cin >> n;
  LL arr[n];
  foro(i, n) { cin >> arr[i]; }
  LL sum = 0;
  LL totalsum = 0;
  foro(i, n) { totalsum = (totalsum + arr[i]); }
  foro(i, n) {
    totalsum = totalsum - arr[i];
    sum = (sum + ((totalsum % mod) * (arr[i] % mod)) % mod) % mod;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define frfr                                                                   \\
  freopen("input.text", "r", stdin); // freopen("output.txt","w",stdout)
#define LL long long int
#define wh                                                                     \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define foro(i, n) for (int i = 0; i < n; i++)
#define forn(i, k, n) for (int i = k; i < n; i++)
#define PB push_back
#define mod 1000000007
#define UMP unordered_map<int, int>
int main() {
  LL n;
  cin >> n;
  LL arr[n];
  foro(i, n) { cin >> arr[i]; }
  LL sum = 0;
  foro(i, n) {
    forn(j, i + 1, n) { sum = (sum + (arr[i] * arr[j]) % mod) % mod; }
  }
  cout << (sum % mod) << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

// Solution By SlavicG
#include "bits/stdc++.h"
using namespace std;

#define ll long long

#define forn(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define pb push_back
#define sz(a) (int)a.size()

#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
const int mod = 1e9 + 7;
int32_t main() {
  int n;
  cin >> n;
  int a[n];
  int s = 0;
  forn(i, n) {
    cin >> a[i];
    s += a[i];
  }
  int suffixsum[n];
  int k = 0;
  for (int i = 0; i < n; i++) {
    k += a[i];
    suffixsum[i] = (s - k) % mod;
  }
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    int add = (a[i] * suffixsum[i]) % mod;
    ans = (ans + add) % mod;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

// Solution By SlavicG
#include "bits/stdc++.h"
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define ll long long

#define forn(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define pb push_back
#define sz(a) (int)a.size()

#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
const int mod = 1e9 + 7;
int32_t main() {
  int n;
  cin >> n;
  int a[n];
  forn(i, n) cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int add = (a[i] * a[j]) % mod;
      ans = (ans + add) % mod;
    }
  }
  cout << ans;
}
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 rrep(i, n) for (int i = 1; i <= (n); i++)
using ll = long long;

using namespace std;

const ll mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> smod(n + 1);
  for (int i = n - 1; i >= 0; i--) {
    smod[i] = (smod[i + 1] + a[i]) % mod;
  }
  ll ans = 0;
  rep(i, n - 1) {
    ans += a[i] * smod[i + 1];
    ans %= mod;
  }
  cout << ans % mod << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 1; i <= (n); i++)
using ll = long long;

using namespace std;

const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    rep(j, i) { ans += (a[i] * a[j]) % mod; }
  }
  cout << ans % mod << endl;

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

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  V<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  V<ll> w(n + 1);
  for (int i = 0; i < n; i++) {
    w[i + 1] = (w[i] + v[i]);
  }

  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += (w[n] - w[i + 1]) % mod * v[i] % mod;
    ans %= mod;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  V<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += v[i] * v[j] % mod;
    }
  }
  cout << ans % mod << endl;

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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int64_t MOD_NUM = 1000000000 + 7;
int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> vec(N);
  int64_t sum_ = 0;
  for (int64_t i = 0; i < N; i++) {
    cin >> vec.at(i);
    sum_ += vec[i];
    sum_ %= MOD_NUM;
  }

  int64_t ans = 0;
  for (int64_t i = 0; i < N; i++) {
    sum_ -= vec[i];
    if (sum_ < 0)
      sum_ += MOD_NUM;
    ans += vec[i] * sum_;
    ans %= MOD_NUM;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int64_t MOD_NUM = 1000000000 + 7;
int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> vec.at(i);
  }

  int64_t sum_ = 0;
  vector<vector<int>> kotae_dp(N, vector<int>(N, -1));
  for (int64_t i = 0; i < N - 1; i++) {
    if (vec[i] == 0) {
      continue;
    }

    for (int64_t j = i + 1; j < N; j++) {
      if (kotae_dp[i][j] > -1) {
        sum_ += kotae_dp[i][j];
        continue;
      } else if (kotae_dp[j][i] > -1) {
        sum_ += kotae_dp[j][i];
        continue;
      }

      int64_t atai = vec[i] * vec[j] % MOD_NUM;
      sum_ += atai;
      kotae_dp[i][j] = atai;
      kotae_dp[j][i] = atai;
    }
  }

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

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

// 小数点以下桁数
// fixed << setprecision(i)

int64_t gcd(int64_t a, int64_t b) { return (a % b == 0 ? b : gcd(b, a % b)); }
int64_t lcm(int a, int b) { return (int64_t)a * b / gcd(a, b); }
int factorial(int a) {
  int b = 1;
  while (a) {
    b *= a--;
  }
  return b;
} // 13以下対応
bool is_prime(int a) {
  if (a <= 1)
    return false;
  for (int i = 2; (int64_t)i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int get_adp(double x, int n) {
  return (int)round((x - (int)x) * pow(10, n)) % 10;
} // 小数点以下の指定桁の値を取得
int64_t sigma(int64_t s, int64_t n) { return n * (2 * s + n - 1) / 2; }

// 順列の全列挙
//  vector<int> v(N);
//  iota(v.begin(), v.end(), 1);       // v に 1, 2, ... N を設定
//  do {
//	for(auto x : v) cout << x << " "; cout << "\\n";     // v の要素を表示
//  } while( next_permutation(v.begin(), v.end()) );

// bit全探索
//  for (int tmp = 0; tmp < (1 << ビット数); tmp++) {
//	bitset<ビット数> s(tmp);
//	// (ビット列sに対する処理)
//  }

int main() {
  int64_t ans = 0, c = pow(10, 9) + 7;
  int n;

  cin >> n;
  vector<int64_t> a(n);
  vector<int64_t> sum_a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      sum_a[n - 1 - i] = a[n - 1 - i];
    else
      sum_a[n - 1 - i] = (a[n - 1 - i] + sum_a[n - i]) % c;
  }
  for (int i = 0; i < n - 1; i++) {
    ans += a[i] * sum_a[i + 1];
    ans %= c;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

// 小数点以下桁数
// fixed << setprecision(i)

int64_t gcd(int64_t a, int64_t b) { return (a % b == 0 ? b : gcd(b, a % b)); }
int64_t lcm(int a, int b) { return (int64_t)a * b / gcd(a, b); }
int factorial(int a) {
  int b = 1;
  while (a) {
    b *= a--;
  }
  return b;
} // 13以下対応
bool is_prime(int a) {
  if (a <= 1)
    return false;
  for (int i = 2; (int64_t)i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int get_adp(double x, int n) {
  return (int)round((x - (int)x) * pow(10, n)) % 10;
} // 小数点以下の指定桁の値を取得
int64_t sigma(int64_t s, int64_t n) { return n * (2 * s + n - 1) / 2; }

// 順列の全列挙
//  vector<int> v(N);
//  iota(v.begin(), v.end(), 1);       // v に 1, 2, ... N を設定
//  do {
//	for(auto x : v) cout << x << " "; cout << "\\n";     // v の要素を表示
//  } while( next_permutation(v.begin(), v.end()) );

// bit全探索
//  for (int tmp = 0; tmp < (1 << ビット数); tmp++) {
//	bitset<ビット数> s(tmp);
//	// (ビット列sに対する処理)
//  }

int main() {
  int64_t ans = 0, c = pow(10, 9) + 7;
  int n;

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

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;
ll MODP = 998244353;

long long moduloMultiplication(long long a, long long b, long long c) {
  ll res = 0;
  res = ((a % c) * (b % c)) % c;

  return res;
}
int main() {
  ll sum = 0, n, sum1 = 0;
  cin >> n;
  vector<int> v;
  rep(i, n) {
    int x;
    cin >> x;
    v.push_back(x);
    sum1 += x;
  }
  // cout<<sum1<<endl;
  for (ll i = 0; i < (n - 1); i++) {
    sum1 = (sum1 - v[i]);
    sum += moduloMultiplication(v[i], sum1, MOD);
    //  cout<<sum<<endl;
  }
  cout << sum % MOD << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;
ll MODP = 998244353;

long long moduloMultiplication(long long a, long long b, long long c) {
  ll res = 0;
  res = ((a % c) * (b % c)) % c;

  return res;
}
int main() {
  ll sum = 0, n;
  cin >> n;
  vector<int> v;
  rep(i, n) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  for (ll i = 0; i < (n - 1); i++) {
    for (ll int j = i + 1; j < n; j++) {
      sum += moduloMultiplication(v[i], v[j], MOD);
    }
  }
  cout << sum % MOD << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define rep(a, b, i) for (int i = a; i < b; i++)
#define repi(a, b, i) for (int i = a; i > b; i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define __ ios_base::sync_with_stdio(cin.tie(0) && 0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef double ld;

const ld pi = 3.141592653589793;
const ll inf = 1e18 + 5;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 5;
const int mxn = 1e5 + 5;
const int mx = 1000;

int main() {
  __;

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

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

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define rep(a, b, i) for (int i = a; i < b; i++)
#define repi(a, b, i) for (int i = a; i > b; i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define __ ios_base::sync_with_stdio(cin.tie(0) && 0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef double ld;

const ld pi = 3.141592653589793;
const ll inf = 1e18 + 5;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 5;
const int mxn = 1e5 + 5;
const int mx = 1000;

int main() {
  __;

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

  ll ans = 0;
  rep(0, n, i) {
    rep(i + 1, n, j) { (ans += (1ll * a[i] * a[j]) % MOD) %= MOD; }
  }

  cout << ans << '\\n';

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

#include <iostream>
#include <string>
#include <vector>

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

int main() {
  int N;
  std::cin >> N;
  std::vector<ll> A(N);
  std::vector<ll> c(N);
  rep(i, N) std::cin >> A[i];

  if (N == 2) {
    std::cout << A[0] * A[1] % mod << std::endl;
    return 0;
  }

  c[N - 2] = A[N - 1];
  for (int i = N - 3; i >= 0; i--) {
    c[i] = c[i + 1] + A[i + 1];
    c[i] %= mod;
  }

  ll res = 0;
  rep(i, N - 1) {
    res += (A[i] * c[i]);
    res %= mod;
  }
  std::cout << res << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

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

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

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

typedef long long ll;
#define MOD 1000000007

int main() {
  ll N;
  cin >> N;
  vector<ll> A;
  vector<ll> B(N + 1, 0);
  B[0] = 0;
  for (int i = 0; i < N; ++i) {
    ll temp;
    cin >> temp;
    A.push_back(temp);
    B[i + 1] = B[i] + A[i];
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    ll sum = (B[N] - B[i + 1]) % MOD;
    ans += A[i] * sum;
    ans = ans % MOD;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define MOD 1000000007

int main() {
  ll N;
  cin >> N;
  vector<ll> A;
  for (int i = 0; i < N; ++i) {
    ll temp;
    cin >> temp;
    A.push_back(temp);
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    ans += A[i] *
           (accumulate(A.begin() + i + 1, A.end(), 0,
                       [](int acc, int i) { return (acc + i) % MOD; })) %
           MOD;

    ans = ans % MOD;
  }
  cout << ans << endl;
}
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 dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0)
    res += m;
  return res;
}

typedef pair<ll, ll> pll;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> s(N + 1);
  for (int i = 0; i < N; i++) {
    s[i + 1] = (s[i] + A[i]) % MOD;
  }

  long long ans = 0;
  for (int i = 0; i < N - 1; i++) {
    // cout << A[i] << " " << (s[N] - s[i+1])<< (A[i] * (s[N] - s[i])) << endl;
    ans += ((A[i] % MOD * mod(((s[N] % MOD) - (s[i + 1] % MOD)), MOD)) % MOD) %
           MOD;
    ans %= MOD;
  }
  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)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

typedef pair<ll, ll> pll;

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

  long 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 %= MOD;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long

const ll mod = 1000000007;

using namespace std;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = (sum + (long long)A[i] * A[j]) % 1000000007;
      sum %= (1000000007);
    }
  }
  cout << sum;
}
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;
typedef long long ll;

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

  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    sum -= a[i];
    ll modSum = sum % 1000000007;
    ll tmp = (a[i] * modSum) % 1000000007;
    ans = (ans + tmp) % 1000000007;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ll tmp = (a[i] * a[j]) % 1000000007;
      ans = (tmp + ans) % 1000000007;
    }
  }
  cout << ans << endl;
}