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

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  int64_t MOD = 1e9 + 7;
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  int64_t sum = 0;
  /* O(N^2)
  for (int i = 0; i < N - 1; i++) {
      for (int j = i + 1; j < N; j++) {
          if (i < j && j <= N) sum += (A.at(i) * A.at(j)) % MOD;
      }
  }
  */

  vector<int64_t> B(N + 1);
  for (int i = 0; i < N; i++)
    B.at(i + 1) = (B.at(i) + A.at(i)) % MOD;

  int64_t ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += A.at(i) * (B.at(N) - B.at(i + 1) + MOD);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  int64_t MOD = 1e9 + 7;
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  int64_t sum = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (i < j && j <= N)
        sum += (A.at(i) * A.at(j)) % MOD;
    }
  }
  cout << sum % MOD << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#include <boost/multiprecision/cpp_int.hpp>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using namespace boost::multiprecision;
typedef long long ll;
const int MOD = 1e9 + 7;

int main() {
  ll n;
  cpp_int ans = 0;
  cin >> n;
  vector<cpp_int> a(n), s(n + 1, 0);
  rep(i, n) cin >> a[i];
  rep(i, n) s[i + 1] = s[i] + a[i];
  rep(i, n - 1) {
    cpp_int t = a[i] * (s[n] - s[i + 1]);
    ans = ans + t;
  }
  ans = ans % MOD;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) for (int j = i + 1; j < n; ++j)(ans += a[i] * a[j]) %= MOD;

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

// Only the Creator has given me strength!

#include <cstdio>

#include <sstream>

#include <cstdlib>

#include <cctype>

#include <cmath>

#include <algorithm>

#include <set>

#include <queue>

#include <stack>

#include <list>

#include <iostream>

#include <fstream>

#include <numeric>

#include <string>

#include <vector>

#include <cstring>

#include <map>

#include <iterator>

#include <bitset>

typedef long long ll;

#define pi 3.1415926535897931159979635

#define pb push_back

#define vctr vector<int>

#define vvtr vector<vctr>

using namespace std;

void fastio() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);
}

int main() {

  fastio();

  ll mod = 1e9 + 7, n;

  cin >> n;

  ll num[n], sum[n];

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

    sum[i] = 0;

    cin >> num[i];

    if (i != 0)
      sum[i] = sum[i - 1] + num[i];

    else
      sum[i] = num[i];
  }

  ll ans = 0;

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

    ans = (ans + num[i] * ((sum[n - 1] - sum[i]) % mod)) % mod;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

// Only the Creator has given me strength!

#include <cstdio>

#include <sstream>

#include <cstdlib>

#include <cctype>

#include <cmath>

#include <algorithm>

#include <set>

#include <queue>

#include <stack>

#include <list>

#include <iostream>

#include <fstream>

#include <numeric>

#include <string>

#include <vector>

#include <cstring>

#include <map>

#include <iterator>

#include <bitset>

typedef long long ll;

#define pi 3.1415926535897931159979635

#define pb push_back

#define vctr vector<int>

#define vvtr vector<vctr>

using namespace std;

void fastio() {

  ios::sync_with_stdio(0);

  cin.tie(0);

  cout.tie(0);
}

int main() {

  fastio();

  ll mod = 1e9 + 7, n;

  cin >> n;

  ll num[n], sum = 0;

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

    cin >> num[i];

    if (i != 0)
      sum = (sum + (num[i] * num[0]) % mod) % mod;
  }

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

    for (int j = i + 1; j < n; j++)

      sum = (sum + (num[i] * num[j]) % mod) % mod;
  }

  cout << sum << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, c) for (long long i = 0; i < (long long)c; i++)
const ll INF = 1000000000000001;
const ll af = 1000000007;
int main() {
  string s, t;
  int n;
  ll ans = 0;
  ll wa = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    if (i > 0)
      wa += a[i - 1];
    ans += (a[i] % af) * (wa % af);
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, c) for (long long i = 0; i < (long long)c; i++)
const ll INF = 1000000000000001;
int main() {
  string s, t;
  int n;
  ll ans = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    rep(j, i) {
      ans += a[j] * a[i];
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

// const long long INF = 1LL << 60;
const long long MOD = 1000000007;
using ll = long long;

int main() {
  int N;
  std::cin >> N;
  std::vector<ll> A(N);

  long long plus = 0;
  rep(i, N) {
    int tmp;
    std::cin >> tmp;
    plus += tmp;
    plus = plus % MOD;
    A[i] = tmp;
  }

  ll ans = 0;
  rep(i, N - 1) {
    plus -= A[i];

    // 10億7で割っているので底につくことがある
    if (plus < 0) {
      plus += MOD;
    }
    ans += (plus * A[i]) % MOD;
    ans = ans % MOD;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

// const long long INF = 1LL << 60;
const long long INF = 1000000007;
using ll = long long;

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

  ll ans = 0;

  for (int i = 0; i < N - 1; i++) {
    // jはだんだん減ってく　
    for (int j = i + 1; j < N; j++) {
      ll tmp = A[i] * A[j];
      ans += tmp;
      ans = ans % INF;
    }
  }

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

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

/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

// const long long INF = 1LL << 60;
const long long MOD = 1000000007;
using ll = long long;

int main() {
  int N;
  std::cin >> N;
  std::vector<ll> A(N);

  std::vector<ll> ruisekiwa(N + 1, 0);

  rep(i, N) {
    std::cin >> A[i];
    ruisekiwa[i + 1] = ruisekiwa[i] + A[i];
  }

  long long ans = 0;
  rep(i, N) {
    //********************************************************
    // <EX>
    // in        = { 1, 3, 5, 7 }
    // ruisekiwa = { 0, 1, 4, 9, 16 }
    // i = 1, ruisekiwa = 15 = 16 - 1
    // i = 2, ruisekiwa = 12 = 16 - 4
    // i = 3, ruisekiwa = 7 = 16 - 9
    // i = 0, ruisekiwa = 0 = 16 -16
    //********************************************************
    long long sum =
        (ruisekiwa[N] - ruisekiwa[i + 1]) %
        MOD; // 頭からおしりまで - 今見ているところまで
             // iが大きくなるに従い、ruisekiwa[i+1]の大きさが増えるので
             // sumの大きさは小さくなる
    ans += (A[i] * sum);
    ans %= MOD;
  }

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

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

/* 十 聖イシドールスよ、迷えるプログラマを導き給え！ 十 */
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

// const long long INF = 1LL << 60;
const long long MOD = 1000000007;
using ll = long long;

int main() {
  int N;
  std::cin >> N;
  std::vector<ll> A(N);

  std::vector<ll> ruisekiwa(N + 1, 0);

  rep(i, N) { std::cin >> A[i]; }

  rep(i, N) {
    ruisekiwa[N - i - 1 - 1] = ruisekiwa[N - i - 1] + A[N - i - 1];
    ruisekiwa[N - i - 1] %= MOD;
  }

  long long ans = 0;
  rep(i, N) {
    ans += (A[i] * ruisekiwa[i]) % MOD;
    ans %= MOD;
  }

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

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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 2e5 + 10, M = 1e6 + 10;
ll a[N];
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[i] = (a[i - 1] + x) % mod;
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ll tmp = (a[n] - a[i] + mod) % mod;
    ans = (ans + (a[i] - a[i - 1] + mod) % mod * tmp % mod) % mod;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 1e5 + 10, M = 1e6 + 10;
ll a[N];
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[i] = a[i - 1] + x;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll l = 0, r = 0;
    if (1 <= i - 1)
      l = a[i - 1];
    r = a[n] - a[i];
    ans = (ans + (a[i] - a[i - 1] + mod) * (l + r) % mod) % mod;
  }
  cout << ans * powmod(2, mod - 2) % mod;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1e9 + 7;
int main() {
  ll a, b, c;
  ll l;
  ll n;

  ll i, j, k;
  ll mn = 0;
  cin >> n;
  vector<ll> v;
  ll cnt = 0;
  ll cum[n + 10] = {0};
  for (j = 0; j < n; j++) {
    cin >> a;
    v.push_back(a);
    if (!j)
      cum[j] = a;
    else
      cum[j] = cum[j - 1] + a;
  }
  for (j = 1; j < n; j++) {
    a = cum[j - 1] % mod;
    b = v[j] % mod;
    cnt += (a * b) % mod;
    cnt %= mod;
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:


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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll a, b, c;
  ll l;
  ll n;
  ll mod = 1e9 + 7;
  ll i, j, k;
  ll mn = 0;
  cin >> n;
  vector<ll> v;
  ll cnt = 0;
  for (j = 0; j < n; j++) {
    cin >> a;
    v.push_back(a);
  }
  for (j = 0; j < n; j++) {
    for (i = j + 1; i < n; i++) {
      a = v[i];
      b = v[j];
      cnt = cnt + (a * b) % mod;
      cnt %= mod;
    }
  }
  cout << cnt % mod << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long int> arr(n, 0);
  vector<long long int> pre(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long int mod = 1000000007;
  pre[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    pre[i] = (pre[i + 1] + arr[i + 1]) % mod;
  }
  long long int ans = 0;
  for (long long int i = 0; i < n - 1; i++) {
    ans = (ans + (arr[i] * pre[i]) % mod) % mod;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

const int MOD = pow(10, 9) + 7;

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

  vector<int> A(N);
  vector<long long> S(N + 1, 0);

  for (int i = 0; i < A.size(); i++) {
    cin >> A[i];
    S[i + 1] = S[i] + A[i];
  }

  long answer = 0;

  for (int i = 0; i < A.size() - 1; i++) {
    long long sum = (S[N] - S[i + 1]) % MOD;
    answer += A[i] * sum;
    answer %= MOD;
  }

  cout << answer << endl;
}


Error: TLE

Fixed submission:

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

const int MOD = pow(10, 9) + 7;

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

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

int main() {
  long long n;
  cin >> n;
  vector<long long> av(n);
  long long suma = 0;
  for (auto &a : av) {
    cin >> a;
    suma += a;
  }

  long long ret = 0;
  for (long long i = 0; i < n - 1; ++i) {
    suma -= av[i];
    long long tempa = suma % 1000000007;
    ret += av[i] * tempa;
    ret %= 1000000007;
  }

  cout << ret << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  cin >> n;
  vector<long long> av(n);
  long long suma = 0;
  for (auto &a : av) {
    cin >> a;
    suma += a;
  }

  long long ret = 0;
  for (long long i = 0; i < n - 1; ++i) {
    suma -= av[i];
    if (1000000007 < suma) {
      long long tempa = suma;
      while (1000000007 < tempa) {
        ret += av[i] * 1000000007;
        ret %= 1000000007;
        tempa -= 1000000007;
      }
      ret += av[i] * tempa;
      ret %= 1000000007;
    } else {
      ret += av[i] * suma;
      ret %= 1000000007;
    }
  }

  cout << ret << endl;

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

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll const m = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N), SumAj(N - 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  SumAj[N - 2] = A[N - 1];
  for (int i = N - 3; i >= 0; i--) {
    SumAj[i] = SumAj[i + 1] + A[i + 1];
    SumAj[i] %= m;
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans = (ans + A[i] * SumAj[i]) % m;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

ll const m = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ll SumAj = 0;
    for (int j = i + 1; j < N; j++) {
      SumAj += A[j];
    }
    SumAj %= m;
    ans = (ans + A[i] * SumAj) % m;
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll n;
  cin >> n;
  ll sum = 0;
  vcl a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= mod;
  }
  ll ans = 0;
  for (ll 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;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

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

#include <bits/stdc++.h>
#include <iostream>
#include <set>
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define ll long long
#define ull unsigned long long
#define MAXN 1000000007
#define INF 1e12
using namespace std;
typedef pair<int, int> pi;
typedef pair<pi, int> trip;
typedef pair<ll int, ll int> pli;
typedef vector<int> vi;
typedef vector<ll int> vli;
typedef vector<vi> vvi;
string getString(char x) {
  string s(1, x);

  return s;
}
int highestPowerof2(ll int n) {
  if (n == 0)
    return 0;
  int p = (int)log2(n);
  // cout<<p<<"highest"<<endl;
  return p;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  // int cas=
  // cin>>test;
  while (test--) {
    int n;
    cin >> n;
    vli vec(n);
    vli sum(n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> vec[i];
      vec[i] = vec[i] % 1000000007;
    }
    for (int i = 0; i < n; i++) {
      sum[i + 1] = (sum[i] + vec[i]) % 1000000007;
      // cout<<sum[i+1]<<endl;
    }
    ll int ans = 0;
    for (int i = 0; i < n; i++) {
      // cout<<vec[n]
      ll int k = (((sum[n] + 1000000007) - sum[i + 1])) % 1000000007;
      // cout<<k<<endl;
      k = (k * vec[i]) % 1000000007;
      ans += k;
      ans = ans % 1000000007;
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <set>
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define ll long long
#define ull unsigned long long
#define MAXN 1000000007
#define INF 1e12
using namespace std;
typedef pair<int, int> pi;
typedef pair<pi, int> trip;
typedef pair<ll int, ll int> pli;
typedef vector<int> vi;
typedef vector<ll int> vli;
typedef vector<vi> vvi;
string getString(char x) {
  string s(1, x);

  return s;
}
int highestPowerof2(ll int n) {
  if (n == 0)
    return 0;
  int p = (int)log2(n);
  // cout<<p<<"highest"<<endl;
  return p;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int test = 1;
  // int cas=
  // cin>>test;
  while (test--) {
    int n;
    cin >> n;
    vli vec(n);
    for (int i = 0; i < n; i++) {
      cin >> vec[i];
      vec[i] = vec[i] % 1000000007;
    }
    ll int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        ll int k = (vec[i] * vec[j]) % 1000000007;
        ans += k;
        ans %= 1000000007;
      }
    }
    cout << ans << endl;
  }
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int main() {

  int n;
  cin >> n;
  ll ans = 0, b = 0;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i) {
    ans += (a[i] * b) % mod;
    b = (b + a[i]) % mod;
  }
  cout << ans % mod;
}


Error: TLE

Fixed submission:

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

#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define mod 1000000007
using namespace std;
#define ll long long int

int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll suffix[n];
  suffix[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    suffix[i] = (suffix[i + 1] + a[i]) % mod;
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans = (ans + (a[i] * suffix[i + 1]) % mod) % mod;
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define mod 1000000007
using namespace std;
#define ll long long int
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = (ans + (a[i] * a[j])) % mod;
    }
  }
  cout << ans;
}