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

#include <iostream>
using namespace std;

int main() {
  long n;
  cin >> n;
  long long a[n], s[n + 1];
  s[0] = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    s[i + 1] = s[i] + a[i];
  }

  long long result = 0;
  for (long long i = 0; i < n; i++) {
    result += a[i] * ((s[n] - s[i + 1]) % 1000000007);
    if (result >= 1000000007)
      result %= 1000000007;
  }

  cout << result;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  long long result = 0;
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] == 0)
      continue;
    long long sum = 0;
    for (long long j = i + 1; j < n; j++) {
      if (a[j] == 0)
        continue;
      sum += a[j];
      if (sum > 1000000007)
        sum %= 1000000007;
    }
    result += a[i] * sum;
    if (result > 1000000007)
      result %= 1000000007;
  }

  cout << result;

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

#include <bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\\n'

using namespace std;

const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  ll sum = 0, ans = 0;
  for (int &i : v) {
    cin >> i;
    sum = (sum + i) % MOD;
  }
  for (int i = 0; i < n; ++i) {
    sum -= v[i];
    if (sum < 0)
      sum += MOD;
    ans += v[i] * sum;
    ans %= MOD;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\\n'

using namespace std;

const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n];
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    for (int j = 0; j < i; ++j) {
      ans = (ans + (ll)(a[i]) * (ll)(a[j])) % MOD;
    }
  }
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#define INF INT_MAX
using namespace std;

int N;
int mod = 1000000007;
long long sum;
long ans;

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

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

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>
#define INF INT_MAX
using namespace std;

int N;
vector<int> A(N);
int mod = 1000000007;
long long sum;
long ans;

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

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

  cout << ans << endl;

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

#include <iostream>

using namespace std;

int main() {
  int n;
  const int v = 1000000007;
  cin >> n;
  long long Arr[n], sum = 0, tempsum = 0, arrsum = 0;
  for (int z = 0; z < n; z++) {
    cin >> Arr[z];
  }
  for (int x = 0; x < n; x++) {
    arrsum += Arr[x];
    arrsum %= v;
  }
  for (int x = 0; x < n; x++) {
    arrsum = (arrsum - Arr[x] + v) % v;
    tempsum = (Arr[x]) * (arrsum);
    tempsum %= v;
    sum += tempsum;
    sum %= v;
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  const int v = 1000000007;
  cin >> n;
  long long Arr[n], sum = 0, tempsum = 0;
  for (int z = 0; z < n; z++) {
    cin >> Arr[z];
  }
  for (int x = 0; x < n; x++) {
    tempsum = 0;
    for (int y = x + 1; y < n; y++) {
      tempsum += (Arr[y]) % v;
      tempsum %= v;
    }
    tempsum = ((Arr[x]) * tempsum) % v;
    sum += tempsum;
    sum %= v;
  }
  cout << sum;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vec vector<int>
#define vecll vector<ll>
#define pb(x) push_back(x)

using namespace std;
///////////////////////////////////////////////////////////

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

  ll n;
  cin >> n;

  ll a[n];
  ll sa[n];
  ll in;
  rep(i, n) {
    cin >> in;
    a[i] = in % m;
  }
  sa[0] = a[0];
  for (ll i = 1; i < n; i++) {
    //        cout << sa[i-1] << " " << a[i] << endl;
    sa[i] = sa[i - 1] + a[i];
  }

  ll ans = 0;
  for (ll j = 1; j < n; j++) {
    ans += (sa[j - 1] % m) * (a[j] % m) % m;
    ans %= m;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define rep(i, n) for (ll i = 0; i < n; i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vec vector<int>
#define vecll vector<ll>
#define pb(x) push_back(x)

using namespace std;
///////////////////////////////////////////////////////////

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

  ll n;
  cin >> n;
  ll a[n];
  ll in;
  rep(i, n) {
    cin >> in;
    a[i] = in % m;
  }

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

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

#include <bits/stdc++.h>

#define ll long long
#define pb push_back

using namespace std;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;

const ll mod = (ll)(1e9 + 7);

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;

  vll v(n);
  ll cur = 0;
  ll s = 0;

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

  ll sum[n];
  sum[0] = v[0];

  for (ll i = 1; i < n; i++) {
    sum[i] = sum[i - 1] + v[i];
  }

  for (ll i = 0; i < n; i++) {
    cur = sum[n - 1] - sum[i];

    s = ((s % mod) + (((v[i] % mod) * (cur % mod)) % mod) % mod) % mod;
  }

  cout << s;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define pb push_back

using namespace std;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pi;

const int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;

  vll v(n);
  ll c = 0;

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

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

  cout << c;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; ++i)

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll N;
  ll A[200010];
  cin >> N;
  rep(i, N) { cin >> A[i]; }
  ll ans = 0;
  ll m = 1000000007;
  ll t[200010];

  t[0] = A[N - 1];
  rep(i, N - 2) {
    t[i + 1] = t[i] + A[N - 2 - i];
    t[i + 1] %= m;
  }

  rep(i, N - 1) {
    ans += A[i] * t[N - i - 2];
    ans %= m;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; ++i)

using Graph = vector<vector<int>>;

// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true; // v を訪問済にする

  // v から行ける各頂点 next_v について
  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;     // next_v が探索済だったらスルー
    dfs(G, next_v); // 再帰的に探索
  }
}

bool IsPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

int main() {
  ll N;
  ll A[200010];
  cin >> N;
  rep(i, N) { cin >> A[i]; }
  ll ans = 0;
  ll m = 1000000007;

  rep(i, N) {
    FOR(j, i + 1, N) {
      ans += A[i] * A[j];
      ans %= m;
    }
  }

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

#include <iostream>
#include <vector>
using namespace std;
#define mod 1000000007

int main() {
  int n, x;
  cin >> n >> x;
  pair<long long, long long> v;
  v.second = x;
  v.first = 0;
  for (int i = 1; i < n; i++) {
    cin >> x;
    v.first = (v.first + (v.second * x) % mod) % mod;
    v.second += x;
    v.second %= mod;
  }
  cout << v.first << endl;
}


Error: TLE

Fixed submission:

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

#define modu(x)                                                                \\
  (((x) < (pow(10, 9) + 7)) ? (x) : ((x) % ((long long)(pow(10, 9) + 7))))

int main() {
  int n;
  cin >> n;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long result = 0;
  for (int i = 0; i < n; i++) {
    long long presult = 0;
    for (int j = i + 1; j < n; j++) {
      // cout << result << endl;
      presult += v[j];
    }
    presult = modu(presult);
    result += modu(presult * v[i]);
    result = modu(result);
  }
  cout << result << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

//
int main() {
  int N;
  cin >> N;
  long long a;
  long long modNum = pow(10, 9) + 7;

  long long ans = 0;
  long long total = 0;

  for (int i = 0; i < N; i++) {
    cin >> a;
    ans += total * a;
    ans %= modNum;
    total += a;
    total %= modNum;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

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

//
int main() {
  int N;
  cin >> N;
  long long A[N];
  long long modNum = pow(10, 9) + 7;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];

    for (int j = 0; j < i; j++) {
      ans += A[i] * A[j];
      ans %= modNum;
    }
  }

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

#include <bits/stdc++.h>

long long int MOD = 1e9 + 7;

long long int modPow(long long int x, long long int a) {
  if (a == 1)
    return x;
  if (a % 2)
    return (x * modPow(x, a - 1)) % MOD;
  long long int t = modPow(x, a / 2);
  return (t * t) % MOD;
}

long long int modInv(long long int x) { return modPow(x, MOD - 2); }

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

  std::vector<long long int> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  long long int sum = 0;
  long long int sqs = 0;
  for (int i = 0; i < N; i++) {
    sum = (sum + A[i]) % MOD;
    sqs = (sqs + A[i] * A[i]) % MOD;
  }
  long long int mod = ((sum * sum % MOD - sqs) * modInv(2)) % MOD;
  if (mod < 0) {
    mod += MOD;
  }
  std::cout << mod << std::endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int MAX = 1000000007;
  int N;
  std::cin >> N;

  std::vector<long long int> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  long long int max = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      max = (max + A[i] * A[j]) % MAX;
    }
  }

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

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

int main() {
  int N;
  cin >> N;
  vector<ull> A(N);
  ull A_sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A_sum += A[i];
  }
  ull sum = 0;
  for (int i = 0; i < N - 1; i++) {
    A_sum = A_sum - A[i];
    ull A_sum_mod = A_sum % 1000000007;
    sum += A[i] * A_sum_mod;
    sum = sum % 1000000007;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  long long mod = 1000000007;

  long long sum = 0;
  long long right = A.at(N - 1) % mod;
  for (long long i = N - 2; i >= 0; i--) {
    long long left = A.at(i) % mod;
    sum += (left * right) % mod;
    right += left;
    right = right % mod;
  }

  cout << sum % mod << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
int N;
int modulo = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;

  vector<ll> a(N);
  ll sum = 0, b = 0;

  rep(i, 0, N) { cin >> a[i]; }
  rep(i, 0, N) {
    b += a[i];
    b %= modulo;
  }
  rep(i, 0, N) {
    b -= a[i];
    if (b < 0) {
      b += modulo;
    }
    sum += a[i] * b;
    sum %= modulo;
  }

  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;

  vector<ll> a(N);
  ll sum = 0;
  rep(i, 0, N) { cin >> a[i]; }
  rep(i, 0, N) {
    for (int j = i + 1; j < N; j++) {
      sum = sum + (a[i] % 1000000007) * (a[j] % 1000000007);
      sum %= 1000000007;
    }
  }
  cout << sum << 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 lint long long
const lint mod = 1e9 + 7;
const lint inf = 2.19e15 + 1;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int n;
  lint sum = 0;
  cin >> n;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  vector<lint> acm(n + 1);
  acm[0] = 0;
  REP(i, n) { acm[i + 1] = (acm[i] + a[i]) % mod; }
  REP(i, n) {
    sum += acm[i] * a[i] % mod;
    sum %= mod;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define lint long long
const lint mod = 1e9 + 7;
const lint inf = 2.19e15 + 1;
const long double eps = 1e-10;
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int n;
  lint sum = 0;
  cin >> n;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  REP(i, n - 1) {
    for (int j = i + 1; j < n; j += 1) {
      sum += a[i] * a[j] % mod;
      sum %= mod;
    }
  }
  cout << sum << 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 long> V(N);
  for (int i = 0; i < N; i++) {
    cin >> V.at(i);
  }
  long long C = 1000000007;
  long long Sum = 0;
  vector<long long> W(N - 1);
  W.at(0) = V.at(0);
  for (int i = 1; i < N - 1; i++) {
    W.at(i) = W.at(i - 1) + V.at(i);
  }
  for (int i = 0; i < N - 1; i++) {
    Sum += ((W.at(i) % C) * V.at(i + 1)) % C;
  }
  cout << Sum % C << endl;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll n, m;
  cin >> n;
  m = 1e9 + 7;
  ll a[n], s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += (a[i] % m);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    s -= a[i];
    ans = (ans % m + ((s % m) * (a[i] % m) % m)) % m;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
bool ispal(string a) {
  long long int n = a.length();
  if (n == 1)
    return true;
  else {
    int i = 0, j;
    j = n - 1;
    while (1) {
      if (a[i] != a[j]) {
        cout << i << " " << j;
        return false;
      }
      i++, j--;
      if (i == j) {
        //     cout<<i<<" ";
        break;
      }
    }
    return true;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll m = 1e9 + 7, ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans = ans % m + ((a[i] % m * a[j] % m) % m) % m;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  long long int a[200001];

  long long int res = 0;

  long n;
  cin >> n;
  long long int all = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    all = (all + a[i]) % mod;
  }
  long long int current_sum = all;
  for (int i = 0; i < n; i++) {
    long long int val = 0;
    current_sum = (current_sum - a[i]) % mod;
    if (current_sum < 0) {
      current_sum += mod;
    }
    // cout << current_sum << endl;
    val = (current_sum * a[i]) % mod;
    res = (res + val) % mod;
  }
  res = (res) % (mod);
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  unsigned long long int a[200001];

  unsigned long long int res = 0;

  long n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    unsigned long long int val = 0;
    for (int j = i + 1; j < n; j++) {
      val = (val + a[j]) % mod;
    }
    val = (val * a[i]) % mod;
    res = (res + val) % mod;
  }
  res = (res) % (mod);
  cout << res << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MOD = pow(10, 9) + 7;
using ll = long long;

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

  vector<ll> sum(n);
  ll pre = 0;
  for (int i = 0; i < n; i++) {
    sum[i] = (pre % MOD + a[i] % MOD) % MOD;
    pre = sum[i];
  }
  for (int i = 0; i < n - 1; i++) {
    ans += (sum[i] * a[i + 1] % MOD) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = pow(10, 9) + 7;
using ll = long long;

int main() {
  ll n;
  ll ans = 0;
  cin >> n;
  vector<ll> a(n);
  for (int 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)) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}