alexjercan/bugnet huggingface dataset:
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 per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0LL;
  rep(i, n) sum += a[i];
  ll ans = 0LL;
  rep(i, n) {
    sum -= a[i];
    ll num = sum % mod;
    ans += (num * a[i]);
    ans %= mod;
  }
  if (ans < 0)
    ans += mod;
  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 < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0LL;
  rep(i, n) {
    sum += a[i];
    sum %= mod;
  }
  ll ans = 0LL;
  rep(i, n) {
    ans += ((a[i] * sum) % mod);
    ans %= mod;
  }
  rep(i, n) {
    ans -= a[i] * a[i];
    while (ans < 0)
      ans += mod;
    ans %= mod;
  }
  while (ans < 0)
    ans += mod;
  ans /= 2LL;
  cout << ans << endl;
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

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

#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9;
const ll INF = 1e18;
const ll Mod = (1e9 + 7) * 2;

int n;
ll a[200010];
ll sum, ans;

int main() {
  fast;

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= Mod;

    ans += Mod - a[i] * a[i] % Mod;
    ans %= Mod;
  }

  ans += sum * sum % Mod;
  ans %= Mod;

  cout << ans / 2;
}

Error: WA

Fixed submission:

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

#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9;
const ll INF = 1e18;
const ll Mod = (1e9 + 7) * 2;

int n;
ll a[100010];
ll sum, ans;

int main() {
  fast;

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    sum %= Mod;

    ans += Mod - a[i] * a[i] % Mod;
    ans %= Mod;
  }

  ans += sum * sum % Mod;
  ans %= Mod;

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

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

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  ll q = 7 + 1e9;
  vector<ll> subsum(N);
  subsum[N - 1] = 0;
  for (int i = N - 1; i > 0; --i) {
    subsum[i - 1] = subsum[i] + A[i];
    subsum[i - 1] %= q;
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    ans += A[i] * subsum[i];
    ans %= q;
  }
  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;
using ll = long long;
using P = pair<int, int>;

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

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define pi 2 * acos(0)
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
using namespace std;

int main() {

  io;

  int n;
  cin >> n;

  vector<ll> sum, v;
  ll s = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    s = (s + x) % mod;
    v.push_back(x);
    sum.push_back(s);
  }

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

    ll a = s - sum[i];
    if (a < 0)
      a += mod;

    ans += v[i] * a;
    ans %= mod;

    // ans = (ans + (v[i]*(s-sum[i]))%mod ) % mod;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define io                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define pi 2 * acos(0)
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
using namespace std;

int main() {

  io;

  int n;
  cin >> n;

  vi v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }

  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++)
      ans = (ans + ((ll)v[i] * v[j]) % mod) % mod;
  }
  cout << ans;
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long long int n, sum = 0;
  cin >> n;
  vector<long long int> A(n, 0);
  vector<long long int> B(n, 0);

  for (long long int i = 0; i < n; i++) {
    cin >> A[i];
    B[i] = A[i];
    sum += A[i];
  }

  for (long long int i = 1; i < n; i++)
    A[i] += A[i - 1];

  long long int ans = 0;
  for (long long int i = 0; i < n; i++) {
    ans = (ans + B[i] * ((sum - A[i]) % 1000000007) % 1000000007) % 1000000007;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

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

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

  int n;
  cin >> n;
  vector<long long int> A(n, 0);
  for (long long int i = 0; i < n; i++)
    cin >> A[i];

  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 + A[i] * A[j] % 1000000007) % 1000000007;
    }
  }

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

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

int modinv(long long a, long long m = 1000000007) {
  for (long long i = 0; i < m; i++) {
    if (((i * m + 1) % a) == 0) {
      return i;
    }
  }
  return -1;
}

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

  int answer = 0;
  long long jsum[200010];

  for (int j = n - 1; j >= 0; j--) {
    if (j == n - 1) {
      jsum[j] = a[j];
    } else {
      jsum[j] = (jsum[j + 1] + a[j]) % m;
    }
  }

  for (int i = 1; i < n; i++) {
    answer = (answer + jsum[i] * a[i - 1]) % m;
  }

  cout << answer;
}

Error: TLE

Fixed submission:

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

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

  int answer = 0;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < j; i++) {
      answer = (answer + a[i] * a[j]) % m;
    }
  }

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

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int64_t sum = 0;
  int64_t ksum = 0;
  for (int i = 1; i < N; i++) {
    ksum += vec.at(i);
    ksum = ksum % 1000000007;
  }

  for (int i = 0; i < N - 1; i++) {
    sum += (vec.at(i) * ksum) % 1000000007;
    sum = sum % 1000000007;
    ksum -= vec.at(i + 1);
    if (ksum < 0)
      ksum += 1000000007;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      sum += (vec.at(i) * vec.at(j)) % 1000000007;
      sum = sum % 1000000007;
    }
  }
  cout << sum << endl;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

ll A[200010];
int main() {
  int N;
  cin >> N;
  ll sum = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans = (ans + (sum % mod) * A[i]) % mod;
    sum += A[i];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

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

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  ll N;
  cin >> N;
  vector<ll> p(N);
  ll sum = 0, t = 0;
  froop1(0, N) {
    cin >> p.at(i);
    t = t + p.at(i);
    if (i != 0)
      sum += ((t - p.at(i)) % 1000000007LL) * p.at(i);
    sum %= 1000000007LL;
  }
  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  ll N;
  cin >> N;
  vector<ll> p(N);
  ll sum = 0;
  froop1(0, N) {
    cin >> p.at(i);
    for (int j = 0; j < i; j++) {
      sum += (p.at(i) * p.at(j)) % 1000000007;
      sum %= 1000000007;
    }
  }
  cout << sum << endl;

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

#include <iostream>
using namespace std;
const long mod = 1e9 + 7;
int main() {
  int N;
  cin >> N;
  long S = 0, T = 0;
  for (int i = 0; i < N; i++) {
    long A;
    cin >> A;
    S = (S + A * T) % mod;
    T = (T + A) % mod;
  }
  cout << S << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int MOD = 1000000007;
unsigned long long A[200004];
int main() {
  int N;
  cin >> N;
  unsigned long long ans = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += A[i] * A[j];
      ans %= 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 IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define bs binary_search
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a, b) for (int i = a; i >= b; i--)
#define mem(a, b) memset(a, b, sizeof(a))
#define setprec(x) cout << fixed << setprecision(x);
#define one(x) __builtin_popcount(x)
#define endl "\\n"
#define mod 1000000007

int main() {
  ll int n;
  cin >> n;
  ll int arr[n + 1];
  FOR(i, 0, n) cin >> arr[i];
  ll int ans = 0, sum = 0;
  FOR(i, 0, n) {
    sum += arr[i];
    sum %= mod;
  }
  FOR(i, 0, n - 1) {
    sum -= arr[i];
    if (sum < 0)
      sum += mod;
    ans += arr[i] * sum;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define bs binary_search
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a, b) for (int i = a; i > b; i--)
#define mem(a, b) memset(a, b, sizeof(a))
#define setprec(x, k) cout << fixed << setprecision(x) << k
#define one(x) __builtin_popcount(x)
#define endl "\\n"
#define mod 1000000007

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define io ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
ll a[maxn], pre[maxn]; //,sum[maxn];
const int mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  // ll sum=0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  // sort(a+1,a+1+n);
  // pre[0]=0;
  for (int i = 1; i <= n; i++) {
    pre[i] = pre[i - 1] + a[i];
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ans = (ans + ((pre[n] - pre[i]) % mod) * a[i] % mod) % mod;
  }
  cout << ans % mod;
}
int main() {
  io;
  // int T;cin>>T;
  // while(T--)
  solve();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define io ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll a[maxn];
const int mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      sum = (sum + a[i] * a[j] % mod) % mod;
    }
  }
  cout << sum % mod;
}
int main() {
  io;
  // int T;cin>>T;
  // while(T--)
  solve();
}
Problem p02572 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
static const long long MOD = 1000000007;
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
// 任意長整数型
using Bint = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
using Real = mp::number<mp::cpp_dec_float<1024>>;

int main() {
  long long N;
  cin >> N;
  Bint A[200010];
  Bint sum = 0;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  Bint ans = 0;
  sum -= A[0];
  for (long long i = 0; i < N - 1; i++) {
    // cout<<ans<<" "<<sum<<endl;
    ans += A[i] * sum;
    ans %= MOD;
    sum -= A[i + 1];
  }

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


Error: TLE

Fixed submission:

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

int main() {
  long long N;
  cin >> N;
  long long A[200010];
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  long long ans = 0;
  for (long long i = 0; i < N; i++) {
    for (long long j = i + 1; j < N; j++) {
      // cout<<A[i]*A[j]<<endl;
      ans += A[i] * A[j] % 1000000007;
      ans %= 1000000007;
    }
  }
  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 d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

ll n;
ll a[N];
ll sum = 0, ans = 0;

int main() {
  lscf(n);
  rep(i, 0, n) {
    lscf(a[i]);
    sum = (sum + a[i]) % M;
  }

  rep(i, 0, n - 1) {
    sum = (sum - a[i] + M) % M;
    ans = (ans + a[i] * sum) % M;
  }
  lpri(ans);
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

ll n;
ll a[N];

int main() {
  lscf(n);
  rep(i, 0, n) lscf(a[i]);

  ll sum = 0;

  rep(i, 0, n - 1) { rep(j, i + 1, n) sum = (sum + a[i] * a[j]) % M; }
  lpri(sum);
  return 0;
}
Problem p02572 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <fstream>
#include <iostream>

#define rep(a) for (int i = 0; i < a; i++)
#define E << endl;
#define Mode 1000000007
#define F freopen("in.txt", "r", stdin);
using namespace std;

int main() {
  // F;
  int ar[200005];
  long long tem[200005];
  int n;
  cin >> n;
  rep(n) cin >> ar[i];

  long long sum = 0;
  rep(n) {
    sum += ar[i];
    tem[i] = sum;
  }
  long long ans = 0;
  rep(n - 1) {
    long long tempsum = tem[n - 1] - tem[i];
    tempsum %= Mode;

    // cout<<"just adition "<<tempsum<<endl;

    tempsum *= ar[i];

    tempsum %= Mode;

    ans += tempsum;
    ans %= Mode;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <fstream>
#include <iostream>

#define rep(a) for (int i = 0; i < a; i++)
#define E << endl;
#define Mode 1000000007
#define F freopen("in.txt", "r", stdin);
using namespace std;

int main() {
  // F;
  int ar[20005];
  int tem[20005];
  int n;
  cin >> n;
  rep(n) cin >> ar[i];

  long long sum = 0;
  rep(n) {
    sum += ar[i];
    tem[i] = sum;
  }
  long long ans = 0;
  rep(n - 1) {
    long long tempsum = tem[n - 1] - tem[i];
    tempsum %= Mode;

    // cout<<"just adition "<<tempsum<<endl;

    tempsum *= ar[i];

    tempsum %= Mode;

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

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

// Radhe Radhe
#include <bits/stdc++.h>
#define godspeed                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define ll long long
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define en '\\n'
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, a, b) for (ll i = a; i <= b; i++)
#define repVect(i, n) for (ll i = 0; i < sum.size(); i++)
#define fixed cout.setf(ios::fixed);
#define Precise cout.precision(7);
#define yes cout << "YES" << '\\n'
#define no cout << "NO" << '\\n'
using namespace std;

ll mulmod(ll a, ll b, ll M) {
  if (M <= 1000000009)
    return a * b % M;

  ll res = 0;
  while (a > 0) {
    if (a & 1) {
      res += b;
      if (res >= M)
        res -= M;
    }
    a >>= 1;
    b <<= 1;
    if (b >= M)
      b -= M;
  }
  return res;
}

int main() {
  godspeed ll t, n, m, x, i, j, k, l;
  string s;
  cin >> n;
  ll a[n], sum = 0;
  for (i = 0; i < n; i++)
    cin >> a[i];

  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++) {
      sum += mulmod(a[i], a[j], mod);
    }

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

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

typedef long long ll;

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

  ll sum = 0;
  ll ans = 0;

  for (int i = 0; i < n; i++)
    sum = (sum + a[i]) % 1000000007;

  for (int i = 0; i < n - 1; i++) {
    sum -= a[i];
    if (sum < 0)
      sum += 1000000007;
    ans = (ans + a[i] * sum) % 1000000007;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

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

  int sum = 0;

  for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
      sum = (sum + ((a[i] * a[j]) % 1000000007)) % 1000000007;

  cout << sum;
}