A - アルバイトの給料計算 / Calculating Part-Time Job Pay 解説 by admin
GPT 5.4 HighOverview
For each part-time job, calculate “hourly wage \(\times\) hours worked” and sum them all up to obtain the total salary for this month.
Analysis
The amount received from the \(i\)-th part-time job, given an hourly wage of \(A_i\) yen and working hours of \(T_i\) hours, is
\(A_i \times T_i\)
yen.
Therefore, the desired answer is
\(A_1T_1 + A_2T_2 + \cdots + A_NT_N\)
For example,
- Job 1: hourly wage \(1000\) yen, \(3\) hours → \(3000\) yen
- Job 2: hourly wage \(1200\) yen, \(2\) hours → \(2400\) yen
Then the total is \(3000 + 2400 = 5400\) yen.
Naive Approach
One might consider “adding the hourly wage for each individual hour worked.”
However, since \(T_i\) can be up to \(10^6\) and the number of job types \(N\) can be up to \(10^5\), processing hour by hour would be extremely wasteful.
In practice, it suffices to compute \(A_i \times T_i\) all at once for each job, so we only need to look at each job once.
Algorithm
- Read \(N\).
- Initialize a variable
totalrepresenting the total amount to \(0\). - For each part-time job, read the hourly wage \(A_i\) and working hours \(T_i\).
- Perform
total += A_i * T_i. - Finally, output
total.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Implementation Notes
You can read each line and immediately add
a * tto the total; there is no need to store values in an array.The total amount can be as large as approximately \(10^5 \times 10^6 \times 10^6 = 10^{17}\). Python’s integer type can handle this without any issues.
The provided code uses
sys.stdin.buffer.readlinefor fast input, but the essence of this problem is simply “summing up products.”Source Code
import sys
def main():
input = sys.stdin.buffer.readline
N = int(input())
total = 0
for _ in range(N):
a, t = map(int, input().split())
total += a * t
print(total)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.4-high.
投稿日時:
最終更新: