-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path279BBooks.cpp
More file actions
38 lines (28 loc) · 1.03 KB
/
Copy path279BBooks.cpp
File metadata and controls
38 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
int main() {
int n, t;
cin >> n >> t; // Input: number of books and total free time
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i]; // Read time needed for each book
}
int start = 0, end = 0, maxBooks = 0;
long long sum = 0;
while (end < n) {
sum += a[end]; // Add the time for the current book (at position end)
// If the total time is too much, remove books from the start
while (sum > t) {
sum -= a[start]; // Subtract the time of book at position start
start++; // Move the start pointer forward
}
// Calculate how many books we are reading now
int currentBooks = end - start + 1;
// Update the maximum number of books we can read
if (currentBooks > maxBooks)
maxBooks = currentBooks;
end++; // Move to the next book
}
cout << maxBooks << endl; // Print the result
return 0;
}