// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 5e5 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n; array<ll, 3> a[MAXN];
void Solve() {
cin >> n;
For(i, 1, n) cin >> a[i][2];
For(i, 1, n) cin >> a[i][1];
For(i, 1, n) a[i][0] = a[i][1] + a[i][2];
sort(a + 1, a + n + 1);
priority_queue<ll> pq;
ll sum = 0; int ans = 0;
For(i, 1, n) {
if (sum <= a[i][1]){
sum += a[i][2];
++ans;
pq.push(a[i][2]);
} else if(pq.size() && pq.top() > a[i][2]){
sum -= pq.top();
pq.pop();
sum += a[i][2];
pq.push(a[i][2]);
}
}
cout << ans << '\n';
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("a.inp", "r")) {
freopen("a.inp", "r", stdin);
freopen("a.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("a.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("a.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
18008 KB |
Output is correct |
2 |
Correct |
137 ms |
17104 KB |
Output is correct |
3 |
Correct |
115 ms |
17224 KB |
Output is correct |
4 |
Correct |
118 ms |
16968 KB |
Output is correct |
5 |
Correct |
125 ms |
17228 KB |
Output is correct |
6 |
Correct |
114 ms |
17084 KB |
Output is correct |
7 |
Correct |
112 ms |
16372 KB |
Output is correct |
8 |
Correct |
112 ms |
16968 KB |
Output is correct |
9 |
Correct |
114 ms |
16968 KB |
Output is correct |
10 |
Correct |
117 ms |
17176 KB |
Output is correct |
11 |
Correct |
112 ms |
19052 KB |
Output is correct |
12 |
Correct |
119 ms |
24000 KB |
Output is correct |
13 |
Correct |
119 ms |
19364 KB |
Output is correct |
14 |
Correct |
123 ms |
22216 KB |
Output is correct |
15 |
Correct |
116 ms |
22464 KB |
Output is correct |
16 |
Correct |
122 ms |
17864 KB |
Output is correct |
17 |
Correct |
123 ms |
18632 KB |
Output is correct |
18 |
Correct |
127 ms |
18660 KB |
Output is correct |
19 |
Correct |
123 ms |
18136 KB |
Output is correct |
20 |
Correct |
120 ms |
18128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
612 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
760 KB |
Output is correct |
28 |
Correct |
2 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
600 KB |
Output is correct |
30 |
Correct |
2 ms |
760 KB |
Output is correct |
31 |
Correct |
2 ms |
760 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
2 ms |
592 KB |
Output is correct |
34 |
Correct |
3 ms |
592 KB |
Output is correct |
35 |
Correct |
2 ms |
592 KB |
Output is correct |
36 |
Correct |
2 ms |
592 KB |
Output is correct |
37 |
Correct |
2 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
592 KB |
Output is correct |
42 |
Correct |
2 ms |
488 KB |
Output is correct |
43 |
Correct |
2 ms |
592 KB |
Output is correct |
44 |
Correct |
2 ms |
592 KB |
Output is correct |
45 |
Correct |
2 ms |
760 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
608 KB |
Output is correct |
48 |
Correct |
2 ms |
764 KB |
Output is correct |
49 |
Correct |
2 ms |
612 KB |
Output is correct |
50 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
126 ms |
18008 KB |
Output is correct |
22 |
Correct |
137 ms |
17104 KB |
Output is correct |
23 |
Correct |
115 ms |
17224 KB |
Output is correct |
24 |
Correct |
118 ms |
16968 KB |
Output is correct |
25 |
Correct |
125 ms |
17228 KB |
Output is correct |
26 |
Correct |
114 ms |
17084 KB |
Output is correct |
27 |
Correct |
112 ms |
16372 KB |
Output is correct |
28 |
Correct |
112 ms |
16968 KB |
Output is correct |
29 |
Correct |
114 ms |
16968 KB |
Output is correct |
30 |
Correct |
117 ms |
17176 KB |
Output is correct |
31 |
Correct |
112 ms |
19052 KB |
Output is correct |
32 |
Correct |
119 ms |
24000 KB |
Output is correct |
33 |
Correct |
119 ms |
19364 KB |
Output is correct |
34 |
Correct |
123 ms |
22216 KB |
Output is correct |
35 |
Correct |
116 ms |
22464 KB |
Output is correct |
36 |
Correct |
122 ms |
17864 KB |
Output is correct |
37 |
Correct |
123 ms |
18632 KB |
Output is correct |
38 |
Correct |
127 ms |
18660 KB |
Output is correct |
39 |
Correct |
123 ms |
18136 KB |
Output is correct |
40 |
Correct |
120 ms |
18128 KB |
Output is correct |
41 |
Correct |
2 ms |
592 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
2 ms |
592 KB |
Output is correct |
44 |
Correct |
2 ms |
612 KB |
Output is correct |
45 |
Correct |
2 ms |
592 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
760 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
2 ms |
760 KB |
Output is correct |
51 |
Correct |
2 ms |
760 KB |
Output is correct |
52 |
Correct |
2 ms |
592 KB |
Output is correct |
53 |
Correct |
2 ms |
592 KB |
Output is correct |
54 |
Correct |
3 ms |
592 KB |
Output is correct |
55 |
Correct |
2 ms |
592 KB |
Output is correct |
56 |
Correct |
2 ms |
592 KB |
Output is correct |
57 |
Correct |
2 ms |
592 KB |
Output is correct |
58 |
Correct |
2 ms |
592 KB |
Output is correct |
59 |
Correct |
2 ms |
592 KB |
Output is correct |
60 |
Correct |
2 ms |
592 KB |
Output is correct |
61 |
Correct |
2 ms |
592 KB |
Output is correct |
62 |
Correct |
2 ms |
488 KB |
Output is correct |
63 |
Correct |
2 ms |
592 KB |
Output is correct |
64 |
Correct |
2 ms |
592 KB |
Output is correct |
65 |
Correct |
2 ms |
760 KB |
Output is correct |
66 |
Correct |
2 ms |
592 KB |
Output is correct |
67 |
Correct |
2 ms |
608 KB |
Output is correct |
68 |
Correct |
2 ms |
764 KB |
Output is correct |
69 |
Correct |
2 ms |
612 KB |
Output is correct |
70 |
Correct |
2 ms |
592 KB |
Output is correct |
71 |
Correct |
1 ms |
504 KB |
Output is correct |
72 |
Correct |
1 ms |
336 KB |
Output is correct |
73 |
Correct |
138 ms |
21576 KB |
Output is correct |
74 |
Correct |
129 ms |
21576 KB |
Output is correct |
75 |
Correct |
125 ms |
21668 KB |
Output is correct |
76 |
Correct |
126 ms |
21480 KB |
Output is correct |
77 |
Correct |
126 ms |
21452 KB |
Output is correct |
78 |
Correct |
124 ms |
22400 KB |
Output is correct |
79 |
Correct |
128 ms |
22268 KB |
Output is correct |
80 |
Correct |
125 ms |
22484 KB |
Output is correct |
81 |
Correct |
129 ms |
22216 KB |
Output is correct |
82 |
Correct |
129 ms |
22252 KB |
Output is correct |
83 |
Correct |
138 ms |
23940 KB |
Output is correct |
84 |
Correct |
127 ms |
23928 KB |
Output is correct |
85 |
Correct |
122 ms |
23748 KB |
Output is correct |
86 |
Correct |
123 ms |
23748 KB |
Output is correct |
87 |
Correct |
124 ms |
23796 KB |
Output is correct |
88 |
Correct |
125 ms |
26304 KB |
Output is correct |
89 |
Correct |
116 ms |
26264 KB |
Output is correct |
90 |
Correct |
149 ms |
26216 KB |
Output is correct |
91 |
Correct |
127 ms |
26392 KB |
Output is correct |
92 |
Correct |
117 ms |
26312 KB |
Output is correct |
93 |
Correct |
116 ms |
26048 KB |
Output is correct |
94 |
Correct |
117 ms |
26048 KB |
Output is correct |
95 |
Correct |
130 ms |
26048 KB |
Output is correct |
96 |
Correct |
130 ms |
25880 KB |
Output is correct |
97 |
Correct |
115 ms |
26048 KB |
Output is correct |
98 |
Correct |
123 ms |
21796 KB |
Output is correct |
99 |
Correct |
124 ms |
21736 KB |
Output is correct |
100 |
Correct |
122 ms |
22112 KB |
Output is correct |
101 |
Correct |
146 ms |
25536 KB |
Output is correct |
102 |
Correct |
141 ms |
25352 KB |
Output is correct |