solve in cpp
Criado em: 5 de julho de 2025
Criado em: 5 de julho de 2025
pythonfrom PIL import Image import pytesseract img = Image.open('/mnt/data/image.png') img <PIL.PngImagePlugin.PngImageFile image mode=RGB size=946x914>
pythonimport pytesseract text = pytesseract.image_to_string(img) print(text[:2000])
python# crop middle portion where sample input text appears; coordinates approximate from screenshot size 946x914 w,h=img.size crop = img.crop((0,300, w, 600)) crop <PIL.Image.Image image mode=RGB size=946x300>
pythonprint(pytesseract.image_to_string(crop))
pythoncrop2 = img.crop((0,0, w, 250)) crop2 <PIL.Image.Image image mode=RGB size=946x250>
pythonprint(text)
python# bottom area crop3 = img.crop((0,600, w, h)) crop3 <PIL.Image.Image image mode=RGB size=946x314>
Idea
Treat each bank i
as a possible starting point.
From that start Maku keeps jumping exactly k
positions until she runs past the last bank (i, i+k, i+2k, … < n
).
The sum of candies collected on that path is
pathSum(i) = a[i] + a[i+k] + a[i+2k] + …
The answer is
maxCandies = max { pathSum(i) | 0 ≤ i < n } + c // mom’s initial candies
We can compute every pathSum(i)
in O(n) by scanning from right-to-left:
textdp[i] = a[i] + (i + k < n ? dp[i + k] : 0) best = max(best, dp[i])
dp[i]
is exactly pathSum(i)
because the part after i
is already known when we are walking backwards.
cpp#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n; if (!(cin >> n)) return 0; vector<long long> a(n); for (long long &x : a) cin >> x; long long k, c; cin >> k >> c; vector<long long> dp(n, 0); long long best = LLONG_MIN; for (long long i = n - 1; i >= 0; --i) { dp[i] = a[i]; if (i + k < n) dp[i] += dp[i + k]; best = max(best, dp[i]); } cout << best + c << '\n'; return 0; }
O(n)
O(n)
(can be reduced to O(k)
if needed)