#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define fo(i, a, b) for (int i = a; i >= b; i--)
#define minimize(a, b) a = (a < b ? a : b)
#define maximize(a, b) a = (a > b ? a : b)
#define int long long
#define fi first
#define se second
#define ll long long
#define pii pair <int, int>
#define pb push_back
#define pi acos(-1)
#define sz(a) (int)a.size()
#define getbit(a, b) (((a) >> (b)) & 1)
#define INOUT "main"
#define INFT 0x3f3f3f3f
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
cout << *it << " = [" << a << "]" << '\n';
err(++it, args...);
}
#define debug1(a, st, en) { cout << #a << " = ["; int kt = false; f(i, st, en) {if (kt) cout << ", "; kt = true; cout << a[i];} cout << "]\n"; }
void ctime()
{
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
void file()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
if (fopen(INOUT".INP", "r"))
{
freopen(INOUT".INP", "r", stdin);
freopen(INOUT".OUT", "w", stdout);
}
}
struct NEW
{
int x, g, d;
};
const int MX_N = 1e5 + 5;
int n;
NEW a[MX_N];
ll res = 0;
bool cmp(NEW a, NEW b)
{
return a.x < b.x;
}
bool check(int length)
{
bool kt = false;
ll sum = 0;
int energy = 0;
f(i, 1, length - 1) energy += a[i].d, sum += a[i].g;
for (int st = 0, en = length; en <= n; st ++, en ++)
{
energy = energy - a[st].d + a[en].d;
sum = sum - a[st].g + a[en].g;
if (energy >= a[en].x - a[st].x)
{
kt = true;
if (energy == a[en].x - a[st].x) maximize(res, sum);
}
}
return kt;
}
signed main()
{
file();
cin >> n;
f(i, 1, n) cin >> a[i].x >> a[i].g >> a[i].d, maximize(res, a[i].g);
sort(a + 1, a + n + 1, cmp);
int l = 2, r = n;
while(l <= r)
{
int mid = (l + r) / 2;
if (check(mid)) l = mid + 1;
else r = mid - 1;
}
cout << res;
ctime();
return 0;
}
Compilation message
divide.cpp: In function 'void file()':
divide.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(INOUT".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
divide.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(INOUT".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |