Minimize Cost | Basics of Input/Output & Basic Programming Practice Problems

can someone help me in this problem with o(n) solution