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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  long long int ans = 0;
  int mod = 1000000007;
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    long long int a;
    cin >> a;
    ans += (a * sum) % mod;
    sum += a;
    sum %= mod;
  }
  cout << ans % mod << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

int main() {
  long long N, ans = 0, a;
  long long a_2, sum = 0, sum_2 = 0;
  std::cin >> N;
  for (long long i = 0; i < N; i++) {
    std::cin >> a;
    a_2 = (a * a) % 1000000007;
    sum = (sum + a) % 1000000007;
    sum_2 = (sum_2 + a_2) % 1000000007;
    // std::cout << sum << " " << sum_2 << "\\n";
  }

  ans = ((sum * sum) % 1000000007 - sum_2);
  ans = ans % 1000000007;
  ans = (ans * 500000004) % 1000000007;

  if (ans < 0) {
    ans = 1000000007 + ans;
  }

  std::cout << ans;
}


Error: TLE

Fixed submission:

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

int main() {
  long long N, ans = 0;
  std::cin >> N;
  vector<long long> A(N);

  for (long long i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  for (long long i = 0; i < N; i++) {
    for (long long j = i + 1; j < N; j++) {
      ans = ans + (A[i] * A[j]);
      ans = ans % 1000000007;
    }
  }

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

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

const int MOD = 1000000007;
using ll = long long;

int main() {

  int N;
  cin >> N;

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

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

Error: TLE

Fixed submission:

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

const int MOD = 1000000007;
using ll = long long;

int main() {

  int N;
  cin >> N;

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

  ll ans = 0;
  ll temp;
  for (int i = 0; i < N - 1; i++) {
    temp = 0;
    for (int j = i + 1; j < N; j++) {
      temp += A.at(j);
    }
    temp %= MOD;
    ans += A.at(i) * temp % 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

int main() {
  ll n;
  ll r = 1e9 + 7;
  cin >> n;
  ll arr[n];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    if (i == 0)
      arr[i] = temp;
    else {
      sum += (temp * arr[i - 1]) % r;
      arr[i] = (arr[i - 1] % r + temp % r) % r;
    }
  }

  cout << sum % r;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n;
  ll r = 1e9 + 7;
  cin >> n;
  ll arr[n];
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
    for (ll j = 0; j < i; j++) {
      sum += (arr[i] * arr[j]) % r;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define int long long int
const int mod = 1000000007;
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    sum = sum % mod;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    sum -= arr[i];
    if (sum < 0)
      sum += mod;
    ans += arr[i] * sum;
    ans = ans % mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define int long long int
const int mod = 1000000007;
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum = (sum + (arr[i] * arr[j]) % mod) % mod;
      sum = sum % mod;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

// Write your code here
#include <bits/stdc++.h>
using namespace std;
#include <iostream>

int main() {
  long long int n, ans = 0, m, tmp = 1, x;
  m = 1e9 + 7;
  cin >> n;
  long long int arr[n], prefix[n + 1];
  for (int j = 0; j < n; j++) {
    cin >> x;
    arr[j] = x % m;
  }
  prefix[0] = 0;
  for (int j = 0; j < n; j++) {
    tmp = (prefix[j] + arr[j]);
    prefix[j + 1] = (tmp) % m;
  }
  for (int j = 0; j < n; j++) {
    tmp = prefix[n] - prefix[j + 1];
    tmp = tmp % m;
    tmp = arr[j] * tmp;
    tmp = tmp % m;
    ans = ans + tmp;
    ans = ans % m;
    if (ans < 0) {
      ans = ans + m;
      ans = ans % m;
    }
  }

  cout << ans;
}

Error: TLE

Fixed submission:

// Write your code here
#include <bits/stdc++.h>
using namespace std;
#include <iostream>

int main() {
  long long int n, ans = 0, m, tmp = 1;
  m = 1e9 + 7;
  cin >> n;
  long long int arr[n];
  for (int j = 0; j < n; j++) {
    cin >> arr[j];
  }
  for (int i = 0; i < n; i++) {
    tmp = 1;
    for (int j = i + 1; j < n; j++) {
      tmp = arr[i] * arr[j];
      tmp = tmp + m;
      tmp = tmp % m;
      ans += tmp;
      ans = ans % m;
    }
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  scanf("%d", &n);
  long long res = 0;
  long long a[n];
  long long sum[n] = {0};
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    if (i > 0)
      sum[0] += a[i];
    // sum[0]+=a[i];
    // sum[0]=modul;
  }
  // cout<< sum[0]<< " ";
  for (int i = 1; i < n - 1; i++) {

    sum[i] = sum[i - 1] - a[i];
    // cout<< sum[i]<< " ";
  }
  cout << endl;
  for (int i = 0; i < n - 1; i++) {
    res += ((sum[i] % modul) * a[i]) % modul;
    res %= modul;
  }
  printf("%lld", res);
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  scanf("%d", &n);
  long long res = 0;
  long long a[n];
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      long long ans = (a[i] * a[j]) % modul;
      // cout<< ans << " ";
      res += ans;
      res = res % modul;
    }
  }
  printf("%lld", res);
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n;
  long long *a;
  cin >> n;
  a = new long long[n];
  long long wa = 0;
  ;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    wa += a[i];
    wa %= 1000000007;
  }

  long long result = 0;
  for (int i = 1; i < n; i++) {
    wa -= a[i];
    while (wa < 0) {
      wa += 1000000007;
    }
    result += a[i] * wa;
    result %= 1000000007;
  }

  cout << result << endl;

  delete[] a;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  unsigned long result = 0;
  unsigned long buf;
  for (int i = 0; i < n; i++) {
    buf = 0;
    for (int j = i + 1; j < n; j++) {
      buf += a[j];
    }
    buf %= 1000000007UL;
    result += (buf * a[i]);
    result %= 1000000007UL;
  }

  cout << result << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<long long> ans;
  sum %= (long long)1e9 + 7;
  for (int i = 0; i < n - 1; i++) {
    sum += (long long)1e9 + 7 - a[i];
    sum %= (long long)1e9 + 7;
    ans.push_back(a[i] * sum);
  }
  long long res = 0;
  for (auto x : ans) {
    x %= (long long)1e9 + 7;
    res += x;
  }
  res %= (long long)1e9 + 7;
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> 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];
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#define ll long long
using namespace std;
constexpr auto mod = 1000000000 + 7;
constexpr auto maxn = 200000 + 5;
ll a[maxn];
void exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }

  exgcd(b, a % b, y, x);
  y -= a / b * x;
}
int main() {
  ll n, sum = 0, sum1 = 0, x, y;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    sum1 = (sum1 + (sum - a[i]) % mod * a[i]) % mod;
  }
  exgcd(2, mod, x, y);
  cout << ((x + mod) % mod) * sum1 % mod;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#define ll long long
using namespace std;
constexpr auto mod = 1000000000 + 7;
constexpr auto maxn = 200000 + 5;
ll a[maxn];
int main() {
  ll n, sum = 0, sum1 = 0;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      sum1 = (sum1 + a[i] * a[j] % mod) % mod;
    }
  }
  cout << sum1 << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  long long MOD = 1000000007;

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

  long long res = 0;
  for (int i = 0; i < N; i++) {
    sum -= A.at(i);
    if (sum < 0)
      sum += MOD;
    res += A.at(i) * sum;
    res %= MOD;
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  long long MOD = 1000000007;

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

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int mod = 1000000007;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
    sum %= mod;
  }
  long 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;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> 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 += (long long)a[i] * a[j];
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  int n;
  long long int ans = 0;
  cin >> n;
  const int N = n;
  long long int 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 = (ans + a[i] * a[j]) % MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> aj(n);
  long base = pow(10, 9) + 7;
  aj[n - 1] = a[n - 1] % base;
  for (int i = n - 2; i >= 0; --i) {
    aj[i] = (aj[i + 1] + (a[i] % base)) % base;
  }

  long answer = 0;
  for (int i = 0; i < n - 1; ++i) {
    answer = (answer + (((a[i] % base) * aj[i + 1]) % base)) % base;
  }
  cout << answer << endl;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long base = pow(10, 9) + 7;
  long answer = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      answer = ((((a[i] % base) * (a[j] % base)) % base) + answer) % base;
    }
  }
  cout << answer << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  long long y = 0, z = 0;
  for (int j = 0; j < n; j++) {
    y += x[j];
    z += x[j] * x[j];
  }
  long long m = round(pow(10, 9)) + 7, t = 0;
  for (int k = 0; k < n; k++) {
    t += ((y - x[k]) % m) * x[k] % m;
    t = t % m;
  }
  for (int l = 0; l < m / 10; l++) {
    t = t * 1024 % m;
  }

  cout << t * 32 % m << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  long long y = 0, z = 0;
  for (int j = 0; j < n; j++) {
    y += x[j];
    z += x[j] * x[j];
  }
  long long m = round(pow(10, 9)) + 7, t = 0;
  for (int k = 0; k < n; k++) {
    t += ((y - x[k]) % m) * x[k] % m;
    t = t % m;
  }
  for (int l = 0; l < m - 2; l++) {
    t = t * 2 % m;
  }
  cout << t << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
Author: Vishesh Gupta
E mail: visheshguptavgr@gmail.com
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define newline ("\\n")
#define deb(n) cerr << #n << " = " << (n) << '\\n'
#define FOR(i, n) for (int(i) = 0; (i) < n; (i)++)
#define PI (3.141592653589)

#define ordered_set                                                            \\
  tree<int, null_type, std::less<int>, rb_tree_tag,                            \\
       tree_order_statistics_node_update>

#define MOD 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(1);
  // start coding here

  int n;
  long long ans(0), sum(0);
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  for (int i = 0; i < n; i++) {
    sum = (sum % MOD - arr[i] % MOD + MOD) % MOD;
    ans = ans + ((arr[i] % MOD) * (sum % MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans << newline;

  return 0;
}


Error: TLE

Fixed submission:

/*
Author: Vishesh Gupta
E mail: visheshguptavgr@gmail.com
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define newline ("\\n")
#define deb(n) cerr << #n << " = " << (n) << '\\n'
#define FOR(i, n) for (int(i) = 0; (i) < n; (i)++)
#define PI (3.141592653589)

#define ordered_set                                                            \\
  tree<int, null_type, std::less<int>, rb_tree_tag,                            \\
       tree_order_statistics_node_update>

#define MOD 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(1);
  // start coding here

  int n;
  unsigned long long sum(0);
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      unsigned long long z = 1;
      z = (z * arr[i]) % MOD;
      z = (z * arr[j]) % MOD;
      sum += z;
    }
  }
  cout << sum % MOD << newline;

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

#include <bits/stdc++.h>

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define Ld long double

using namespace std;
typedef long long ll;
const int mx = 1001;
const int mod = 1e9 + 7;
int a[mx];
vector<ll> v;

int main() {
  optimize();
  ll n;
  cin >> n;
  vector<ll> arr(n);
  FOR(i, 0, n - 1) cin >> arr[i];

  ll sum = 0;
  ll prefixSum[n];

  prefixSum[0] = 0;
  prefixSum[1] = arr[0];
  FOR(i, 2, n - 1) prefixSum[i] = (prefixSum[i - 1] + arr[i - 1]) % mod;

  FOR(i, 0, n - 1) {
    sum += prefixSum[i] * arr[i];
    sum %= mod;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define optimize()                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define Ld long double

using namespace std;
typedef long long ll;
const int mx = 1001;
const int mod = 1e9 + 7;
int a[mx];
vector<ll> v;

int main() {
  optimize();
  ll n;
  cin >> n;
  vector<ll> arr(n);
  int k = 0;
  FOR(i, 0, n - 1) {
    ll x;
    cin >> x;
    arr[i] = x;
    for (int j = k - 1; j >= 0; j--) {
      v.push_back(x * arr[j]);
    }
    k++;
  }
  ll sum = 0;
  for (auto vl : v) {
    sum += vl;
    if (sum >= mod)
      sum = sum % mod;
  }
  cout << sum << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
// typedef
using ll = long long;
using str = string;
using PL = pair<ll, ll>;
using P = pair<int, int>;
using VL = vector<ll>;
using VI = vector<int>;
using VS = vector<str>;
using VP = vector<P>;
using VPL = vector<PL>;
using VVL = vector<VL>;
using VVI = vector<VI>;

// const num
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
const long double PI = acosl(-1.0);

// loop
#define REP(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define RREP(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)

// YesNo
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
// All
#define ALL(v) v.begin(), v.end()
#define deduplicate(v) (v).erase(unique(ALL(d)), (v).end())

int main(void) {
  int n;
  cin >> n;
  VL a(n);
  VL sum(n + 1, 0);
  ll ans = 0;
  rep(i, n) { cin >> a[i]; }
  rep(i, n) { sum[i + 1] = sum[i] + a[i]; }
  rep(i, n) {
    ll intvsum = (sum[n] - sum[i + 1]) % MOD;
    ans += a[i] * intvsum;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// typedef
using ll = long long;
using str = string;
using PL = pair<ll, ll>;
using P = pair<int, int>;
using VL = vector<ll>;
using VI = vector<int>;
using VS = vector<str>;
using VP = vector<P>;
using VPL = vector<PL>;
using VVL = vector<VL>;
using VVI = vector<VI>;

// const num
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
const long double PI = acosl(-1.0);

// loop
#define REP(i, a, b) for (ll(i) = (a); (i) < (b); (i)++)
#define RREP(i, a, b) for (ll(i) = (a); (i) >= (b); (i)--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)

// YesNo
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
// All
#define ALL(v) v.begin(), v.end()
#define deduplicate(v) (v).erase(unique(ALL(d)), (v).end())

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

#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
const int M = 1e9 + 7;
long long n, p, q, r, x;
long long f(long long x, long long y) {
  long long k = 1;
  while (y) {
    if (y & 1)
      k = ((k % M) * (x % M)) % M;
    y >>= 1;
    x = ((x % M) * (x % M)) % M;
  }
  if (k < 0)
    k += M;
  return k;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    p += x;
    p %= M;
    q += x * x;
    q %= M;
  }
  r = f(2, M - 2);
  p = ((p % M) * (p % M)) % M;
  if (p < 0)
    p += M;
  p = ((p % M) - (q % M)) % M;
  p = ((p % M) * (r % M)) % M;
  ;
  if (p < 0)
    p += M;
  cout << p;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
const int M = 1e9 + 7;
long long n, p, q, r, x;
long long f(long long x, long long y) {
  long long k = 1;
  while (y) {
    if (y & 1)
      k = ((k % M) * (x % M)) % M;
    y >>= 1;
    x = ((x % M) * (x % M)) % M;
  }
  if (k < 0)
    k += M;
  return k;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    p += x;
    while (p >= M)
      p -= M;
    q += x * x;
    while (q >= M)
      q -= M;
  }
  r = f(2, M - 2);
  p *= p;
  while (p >= M)
    p -= M;
  p -= q;
  while (p < 0)
    p += M;
  p *= r;
  while (p >= M)
    p -= M;
  while (p < 0)
    p += M;
  cout << p;
  return 0;
}