I was solving this problem from the last 2 hour almost ->
1334C - Circle of MonstersI made quite submissions but I was getting TLE for my code.
Then I looked for some red coders solution and the thing is the logic and implementation was same, but I still submit the red coder's code and I'm surprised that IT STILL GOT TLE WHY ?
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
void solve() {
ll n; cin >> n;
vector<ll> a(n), b(n);
for (ll i = 0; i < n; i++)
cin >> a[i] >> b[i];
vector<ll> cost(n, 0);
for (ll i = 0; i < n; i++)
cost[i] = max(0LL, a[i] - b[(i + n - 1) % n]);
ll sum = accumulate(cost.begin(), cost.end(), 0LL);
ll ans = (ll)1e18;
for (ll i = 0; i < n; i++) {
ans = min(ans, sum - cost[i] + a[i]);
}
cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll tc;
cin >> tc;
while (tc--)
solve();
return 0;
}
I don't know what's the problem in this very simple and clean code and still I'm facing this issue.
Can anyone please consider this problem and tell me, why it is giving me TLE ?