A - 屋台の営業日数 / Number of Days a Food Stall Is Open Editorial by admin
GPT 5.2 HighOverview
Focus only on the difference between the daily “total sales” and “total labor costs.” If the funds keep decreasing, calculate how many days the business can last. If they don’t decrease, the business can operate indefinitely (output -1).
Analysis
After operating for one day, the funds change by
\(+\left(\sum_{i=1}^{N}A_i\right)-\left(\sum_{j=1}^{M}B_j\right)\)
Here, let
- Total sales \(X=\sum A_i\)
- Total labor costs \(Y=\sum B_j\)
Then the daily change is \(X-Y\) yen.
Key Insight
Since the daily change is constant, there is no need to simulate day by day.
- If \(X \ge Y\), the daily change \(X-Y\) is non-negative
→ The funds never decrease (they either increase or stay the same)
→ Since the initial funds \(S \ge 0\), the business can operate indefinitely (answer is-1) - If \(X < Y\), the funds decrease by \(d=Y-X>0\) each day
→ After \(k\) days, the funds are \(S-kd\)
→ Since the funds must not go below \(0\) after operation (after paying salaries):
\(S-kd \ge 0 \;\Rightarrow\; k \le \left\lfloor \dfrac{S}{d}\right\rfloor\)
Therefore, the maximum number of days is \(\left\lfloor \dfrac{S}{d}\right\rfloor\).
Why a Naive Approach Fails
If you simulate by subtracting one day at a time until the funds run out, \(S\) can be up to \(10^{18}\), meaning the loop could run up to \(10^{18}\) times, which is not feasible (TLE).
Concrete Example
- Total sales \(X=100\), total labor costs \(Y=130\), initial funds \(S=1000\)
\(d=30\), so the maximum number of days is \(1000//30=33\) days
(After 33 days: \(1000-33\cdot 30=10\); after 34 days: \(-20\), which is not allowed)
Algorithm
- Compute \(X=\sum_{i=1}^{N}A_i\) and \(Y=\sum_{j=1}^{M}B_j\)
- If \(X \ge Y\), output
-1(the business can operate indefinitely) - Otherwise, let \(d=Y-X\) and output \(\left\lfloor \dfrac{S}{d}\right\rfloor\)
Complexity
- Time complexity: \(O(N+M)\) (just computing the sums of the arrays)
- Space complexity: \(O(1)\) (constant space aside from the input arrays)
Implementation Notes
Since \(S\) can be up to \(10^{18}\), computations require 64-bit integers (Python’s
inthandles this safely)Always check
sumA >= sumBfirst and handle the infinite case by outputting-1When funds are decreasing, the answer is simply
S // d(floor division)Source Code
import sys
def main():
input = sys.stdin.readline
N, M, S = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sumA = sum(A)
sumB = sum(B)
if sumA >= sumB:
print(-1)
else:
d = sumB - sumA
print(S // d)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: