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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<ll> vec(n);
  ll sum = 0;
  ll mod = 1000000007;
  rep(i, n) {
    cin >> vec.at(i);
    sum += vec.at(i);
    sum %= mod;
  }
  ll ans;
  for (int i = 0; i < n - 1; i++) {
    sum -= vec.at(i);
    if (sum < 0)
      sum += mod;
    ans += vec.at(i) * sum % mod;
  }
  cout << ans % mod << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, n) cin >> vec.at(i);
  ll sum = 0;
  ll mod = 1000000007;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += vec.at(i) * vec.at(j) % mod;
    }
  }
  cout << sum % mod << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long int
#define endl "\\n"
#define arrinp(v)                                                              \\
  for (auto &i : v)                                                            \\
    cin >> i;
#define arrout(v)                                                              \\
  for (auto &i : v)                                                            \\
    cout << i << " ";
#define newline cout << endl;
using namespace std;
const int MOD = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int p[n];
    p[0] = a[0];
    for (int i = 1; i < n; i++) {
      p[i] = a[i] + p[i - 1];
      // cout<<p[i]<<" ";
    }
    // newline;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += ((((p[i] % MOD) - (a[i] % MOD)) % MOD * a[i] % MOD) % MOD);
      ans = (ans + MOD) % MOD;
    }
    cout << ans % MOD << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define endl "\\n"
#define arrinp(v)                                                              \\
  for (auto &i : v)                                                            \\
    cin >> i;
#define arrout(v)                                                              \\
  for (auto &i : v)                                                            \\
    cout << i << " ";
#define newline cout << endl;
using namespace std;
const int MOD = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    int ans = 0;
    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 = ans % MOD;
    cout << ans << endl;
  }
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n;
  cin >> n;
  ll arr[n], i, j, t, sum[n], ans = 0;
  for (i = 0; i < n; i++)
    cin >> arr[i];

  t = 0;
  for (i = n - 1; i >= 0; i--) {
    t = t + arr[i];
    sum[i] = t % mod;
  }

  for (i = 0; i < (n - 1); i++) {
    t = (arr[i] * sum[i + 1]) % mod;
    ans = (ans + t) % mod;
  }

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


Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  ll arr[n], i, j, t;
  for (i = 0; i < n; i++)
    cin >> arr[i];

  ll sum = 0;
  for (i = 0; i < n; i++) {
    for (j = (i + 1); j < n; j++) {
      t = (arr[i] * arr[j]) % mod;
      sum = (sum + t) % mod;
    }
  }
  cout << sum << endl;
  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<int, int>;
#define rep(i, n) for (int i = 0; i < (n); ++i)

const ll mod = 1000000007;

int main() {
  ll n;
  std::cin >> n;
  std::vector<ll> a(n);

  ll sum = 0;
  rep(i, n) {
    std::cin >> a[i];
    sum += a[i];
    sum %= mod;
  }

  // std::cout << sum << '\\n';

  ll ans = 0;
  for (int i = n - 1; i >= 1; i--) {

    sum -= a[i];
    if (sum < 0)
      sum += mod;
    //
    // std::cout << a[i]<<"*"<<sum<< '\\n';
    // std::cout << a[i]*sum << '\\n';

    ll add = (a[i] * sum) % mod;
    ans += add;
    ans %= mod;
  }

  std::cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

const ll mod = 1e9 + 7;

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

  ll sum = 0;
  rep(i, n) {
    for (int k = i + 1; k < n; k++) {
      sum += (a[i] * a[k]) % mod;
      sum %= mod;
    }
  }
  std::cout << sum << '\\n';

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

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

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

  long ans = 0;

  // i について全探索する
  for (int i = 0; i < N; i++) {
    // A[i+1] ... A[N] の値を更新する
    sum -= A[i];
    if (sum < 0)
      sum += mod;

    ans += A[i] * sum;
    ans %= mod;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const long int pMod = 1000000007;

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

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

ll MOD = 1000000007;

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

Error: TLE

Fixed submission:

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

ll MOD = 1000000007;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 1000000007
using namespace std;
long long a[1100000];
long long b[1100000];
long long n, ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n - 1; i >= 1; i--)
    b[i] = (b[i + 1] + a[i + 1]) % N;
  for (int i = 1; i <= n - 1; i++)
    ans = ((ans % N) + (a[i] * b[i] % N)) % N;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 1000000007
using namespace std;
long long a[1100000];
long long n, ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n - 1; i++)
    for (int j = i + 1; j <= n; j++)
      ans = ((ans % N) + (a[i] * a[j] % N)) % N;
  cout << ans << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

// include
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
// using
using namespace std;
/*using vi = vector <int>;
using vs = vector <string>;
using vc = vector <char>;*/
// define
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define print(n) cout << n << endl;

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

  int b = 0;

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

    a -= A[i];
    if (a < 0)
      a += mod;

    b += A[i] * a;
    b %= mod;
  }

  cout << b << endl;
}


Error: TLE

Fixed submission:

// include
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
// using
using namespace std;
/*using vi = vector <int>;
using vs = vector <string>;
using vc = vector <char>;*/
// define
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define print(n) cout << n << endl;
// グローバル変数宣言
int n = 0, b = 0, c = 0, d = 0;

signed main() {
  cin >> n;
  vector<long long> a(n);
  rep(i, n) { cin >> a[i]; }
  b = 1000000007;

  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      c += a[j];
      c = c % b;
    }

    d = d + (a[i] * c) % b;
    c = 0;
    d = d % b;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define co cout
#define ci cin
#define sf1(n) scanf("%lld", &n)
#define sf2(n, m) scanf("%lld %lld", &n, &m);
#define sf3(n, m, p) scanf("%lld %lld %lld", &n, &m, &p);
#define pf1(n) printf("%lld\\n", n);
#define mem(a, b) memset(a, b, sizeof(a))
#define en cout << endl;
#define pb push_back
#define gcd(n, m) __gcd(n, m)
#define lcm(n, m) ((n) / __gcd(n, m)) * (m)
#define fi first
#define se second
#define coy cout << "YES" << endl;
#define con cout << "NO" << endl;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pi pair<ll, ll>
#define br break;
#define i64 long long
#define PI 2 * acos(0.0)
#define MAXN 3 * 100000

ll findProductSum(vector<ll> A, ll n) {
  ll sum = 0, m = 1000000007, ans = 0;
  for (auto x : A)
    sum = (sum + x) % m, sum %= m;
  for (auto x : A) {
    sum -= x;
    if (sum < 0)
      sum += m;
    ans += (x * sum) % m;
    ans %= m;
  }
  return ans;
}
int main() {
  ll n, m, k, g, mx, mn, res, c, x, y, z, w, p, q, r, l, s, t;
  ci >> n;
  vector<ll> a(n);
  for (auto &x : a)
    ci >> x;
  res = findProductSum(a, n);
  co << res;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define co cout
#define ci cin
#define sf1(n) scanf("%lld", &n)
#define sf2(n, m) scanf("%lld %lld", &n, &m);
#define sf3(n, m, p) scanf("%lld %lld %lld", &n, &m, &p);
#define pf1(n) printf("%lld\\n", n);
#define mem(a, b) memset(a, b, sizeof(a))
#define en cout << endl;
#define pb push_back
#define gcd(n, m) __gcd(n, m)
#define lcm(n, m) ((n) / __gcd(n, m)) * (m)
#define fi first
#define se second
#define coy cout << "YES" << endl;
#define con cout << "NO" << endl;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pi pair<ll, ll>
#define br break;
#define i64 long long
#define PI 2 * acos(0.0)
#define MAXN 3 * 100000

int main() {
  ll n, m, k, g, mx, mn, res, c, x, y, z, w, p, q, r, l, s, t;
  ci >> n;
  vector<ll> a(n);
  for (auto &x : a)
    ci >> x;
  m = 1000000007;
  res = 0;
  f(i, 0, n) {
    f(j, i + 1, n) {
      x = (a[i] * a[j]) % m;
      res += x;
      res %= m;
    }
  }
  co << res;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;

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

  vector<long long> A(N);
  ll sum = 0;
  ll mod = 1e9 + 7;
  REP(i, N) {
    cin >> A[i];
    sum += A[i] % mod;
    sum %= mod;
  }

  res = 0;
  REP(i, N - 1) {
    sum -= A[i];
    if (sum < 0) {
      sum += mod;
    }
    res += (sum * A[i]) % mod;
    res %= mod;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

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

  vector<long long> A(N);
  REP(i, N) { cin >> A[i]; }

  res = 0;
  REP(i, N - 1) {
    FOR(j, i + 1, N) {
      res += (A[i] * A[j]) % ((long long)1e+9 + 7);
      res = res % ((long long)1e+9 + 7);
    }
  }
  cout << res << 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 < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a.at(i);
  ll ans = 0;
  ll sum = 0;
  rep(i, n) {
    sum += a.at(i);
    sum %= 1000000007;
  }
  for (int i = 0; i < n; i++) {
    sum -= a.at(i);
    if (sum < 0)
      sum += 1000000007;
    ans += (ll)a.at(i) * sum;
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a.at(i);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (i == j)
        continue;
      ans += (ll)a.at(i) * a.at(j);
      ans %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int mod = 1e9 + 7;

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

  vector<long long> pref(n);
  for (int i = 0; i < n; i++) {
    if (i == 0)
      pref[i] = a[i];
    else
      pref[i] = pref[i - 1] + a[i];
  }

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

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  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++) {
      long long res = a[i] * a[j];
      if (res > 1000000007)
        res = res % 1000000007;
      long long sum = res + ans;
      if (sum > 1000000007)
        ans = (sum) % 1000000007;
      else
        ans = sum;
    }
  }
  cout << ans << endl;

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

#include <cstdio>
#include <iostream>
#define mod 1000000007
using namespace std;
unsigned long long a[1000005], cnt, sum;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  for (int i = 1; i <= n; i++) {
    sum = sum - a[i];
    cnt = (cnt + (sum % mod * a[i]) % mod) % mod;
  }
  cout << cnt << endl;
  return 0;
}
/* 1 2 3 4 5
2 3 4 5 6 8 10 12 15 20
*/

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#define mod 1000000007
using namespace std;
unsigned long long a[100005], cnt, sum;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
  for (int i = 1; i <= n; i++) {
    sum = sum - a[i];
    cnt = (cnt + (sum % mod * a[i]) % mod) % mod;
  }
  cout << cnt << endl;
  return 0;
}
/* 1 2 3 4 5
2 3 4 5 6 8 10 12 15 20
*/
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

double eps = 1e-9;
int change(string a, string b) {
  int cnt = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i]) {
      cnt++;
    }
  }
  return cnt;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  ll m = 1e9 + 7;
  cin >> n;
  ll arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll pref[n];
  pref[0] = arr[0] % m;
  for (int i = 1; i < n; i++) {
    pref[i] = (arr[i] % m + pref[i - 1]) % m;
  }
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    ans += (arr[i] % m * (pref[n - 1] % m - pref[i + 1] % m + arr[i + 1] % m) %
            m) %
           m;
  }
  cout << ans % m << endl;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

double eps = 1e-9;
int change(string a, string b) {
  int cnt = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] != b[i]) {
      cnt++;
    }
  }
  return cnt;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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



#include <bits/stdc++.h>
#include <string.h>
#include <vector>
// s#define rep(i, n) for (int i = 0; i < (n); i++)
#define MAX 10000

using namespace std;

#define inf 0x3f3f3f3f
#define ll long long
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
typedef vector<int> vi;
typedef vector<bool> vb;
#define INF (int)1e15
#define MOD 1000000007

#define access(i) a[i / 3][i % 3]
// #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;})

int main() {

  int n;
  cin >> n;
  vector<int> v(n);

  ll sum = 0;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
    sum %= MOD;
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    sum -= v[i];

    if (sum < 0)
      sum += MOD;

    ans += v[i] * sum;
    ans = ans % MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <sstream>

// s#define rep(i, n) for (int i = 0; i < (n); i++)
#define MAX 10000

using namespace std;

#define ll long long
#define pb push_back
// #define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define INF (int)1e15
#define MOD 1000000007
#define re0 return 0
// #define N 12
#define sz 1000 * 1000

string multiply(string num1, string num2) {
  int len1 = num1.size();
  int len2 = num2.size();
  if (len1 == 0 || len2 == 0)
    return "0";

  vector<int> result(len1 + len2, 0);

  int i_n1 = 0;
  int i_n2 = 0;

  // Go from right to left in num1
  for (int i = len1 - 1; i >= 0; i--) {
    int carry = 0;
    int n1 = num1[i] - '0';

    i_n2 = 0;

    for (int j = len2 - 1; j >= 0; j--) {

      int n2 = num2[j] - '0';

      int sum = n1 * n2 + result[i_n1 + i_n2] + carry;

      // Carry for next iteration
      carry = sum / 10;

      // Store result
      result[i_n1 + i_n2] = sum % 10;

      i_n2++;
    }

    // store carry in next cell
    if (carry > 0)
      result[i_n1 + i_n2] += carry;

    i_n1++;
  }

  // ignore '0's from the right
  int i = result.size() - 1;
  while (i >= 0 && result[i] == 0)
    i--;

  // If all were '0's - means either both or
  // one of num1 or num2 were '0'
  if (i == -1)
    return "0";

  // generate the result string
  string s = "";

  while (i >= 0)
    s += std::to_string(result[i--]);

  return s;
}

int main() {

  ll n;
  cin >> n;
  vector<string> v(n);

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

  long long sum = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      // cout<<v[i]<<" "<<v[j]<<endl;
      string add = multiply(v[i], v[j]);
      //  cout<<add<<" "<<i<<"  "<<j<<endl;

      int64_t x = 0, fact = 1;
      for (int i = add.length() - 1; i >= 0; i--) {
        x += (add[i] - '0') * fact;
        fact = fact * 10;
      }

      x = x % MOD;

      sum += x;
      sum = sum % MOD;
    }
  }

  cout << sum << endl;

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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
/*vector<Type> v;
vector<Type> v();
vector<Type> v(n);
vector<Type> v(n, d);
vector<vector<Type>> vv;
vector<vector<Type>> vv();
vector<vector<Type>> vv(n);
vector<vector<Type>> vv(n, vector<Type>(m));
vector<vector<Type>> vv(n, vector<Type>(m, d));*/

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  long long Asum = 0;
  long long mod = pow(10, 9) + 7;
  rep(i, N) {
    cin >> A[i];
    Asum += A[i];
    Asum %= mod;
  }
  long long sum = 0;
  for (int i = 0; i < N - 1; i++) {
    Asum -= A[i] % mod;
    if (Asum < 0)
      Asum += mod;
    sum += (A[i] * Asum) % mod;
    sum %= mod;
  }
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
/*vector<Type> v;
vector<Type> v();
vector<Type> v(n);
vector<Type> v(n, d);
vector<vector<Type>> vv;
vector<vector<Type>> vv();
vector<vector<Type>> vv(n);
vector<vector<Type>> vv(n, vector<Type>(m));
vector<vector<Type>> vv(n, vector<Type>(m, d));*/

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) { cin >> A[i]; }
  long long sum = 0;
  long long mod = pow(10, 9) + 7;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += (A[i] * A[j]) % mod;
      if (sum >= mod)
        sum -= mod;
    }
  }
  cout << sum;
  return 0;
}