You are working in Samara, Russia for a few days. Each day has a new pay per unit of work and a new cost per unit of food. Working 1 unit costs 1 unit of energy, and eating 1 unit of food adds 1 unit of energy. Here are some specifications of your employment:

- You arrive with no money, but with energy.
- You can never have more energy than you arrive with, and it can never be negative.
- You can do any amount of work every day (possibly not do any work at all), limited only by your energy.
- You cannot work when your energy is zero. You can eat any amount of food every day (possibly not have any food at all), limited by the money you have.
- You cannot eat when the money you have is zero. You can eat food at the end of the day, and cannot return to work after eating.
- You can return to work the next day.

Your true goal is to return home with as much money as possible. Compute the maximum amount of money you can take home.

**Examples:**

Input:N = 3, Earning=[1, 2, 4], Cost[]=[1, 3, 6], E = 5Output:20Explanation:

Day 1: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go to work this day.

Day 2: 1 unit work earns 2, and 1 unit food costs 3. Thus, you spend more to eat then the total earning so there is no financial incentive to go to work on this day.

Day 3: You earn 4 units per unit of work. The cost of food is irrelevant this day, as you are leaving for the home straight from work.

You spend all of your energy working, the pay is 5 × 4 = 20 units of money and go home without buying dinner.

Input:N=2, Earning=[1, 2], Cost=[1, 4], E=5Output:Total Profit = 0 + 10 = 10Explanation:

First Day: Skip

Second Day: 5*2=10

**Approach: **The approach is to traverse the given **earning** array and the **cost** array and calculate the net profit for each day. Below are the steps:

- For each element in
**earnings[]**and**cost[]**, compare**earning[i]**with**cost[i]**. - If
**earning[i] is less than or equal to cost[i]**, then**skip the work**on that day as the cost of expenses is greater than the earning. Thus, making no profit at all. - If
**earning[i] is greater than to cost[i]**, calculate the total earnings by multiplying earning on that day with total units of energy and then subtract the product of the cost of food on that day and units of energy to calculate profit for that day. - If there is last working day, then calculate the total earnings by multiplying earning on that day with total units of energy and that will be your profit for that day as no more energy is required for work.
- Print the total profit after the above steps.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that calculates the profit ` `// with the earning and cost of expenses ` `int` `calculateProfit(` `int` `n, ` `int` `* earnings, ` ` ` `int` `* cost, ` `int` `e) ` `{ ` ` ` `// To store the total Profit ` ` ` `int` `profit = 0; ` ` ` ` ` `// Loop for n number of days ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `int` `earning_per_day = 0; ` ` ` `int` `daily_spent_food = 0; ` ` ` ` ` `// If last day, no need to buy food ` ` ` `if` `(i == (n - 1)) { ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `profit = profit + earning_per_day; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Else buy food to gain ` ` ` `// energy for next day ` ` ` `if` `(cost[i] < earnings[i]) { ` ` ` ` ` `// Update earning per day ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `daily_spent_food = cost[i] * e; ` ` ` ` ` `// Update profit with daily spent ` ` ` `profit = profit + earning_per_day ` ` ` `- daily_spent_food; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the profit ` ` ` `cout << profit << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given days ` ` ` `int` `n = 4; ` ` ` ` ` `// Given earnings ` ` ` `int` `earnings[] = { 1, 8, 6, 7 }; ` ` ` ` ` `// Given cost ` ` ` `int` `cost[] = { 1, 3, 4, 1 }; ` ` ` ` ` `// Given energy e ` ` ` `int` `e = 5; ` ` ` ` ` `// Function Call ` ` ` `calculateProfit(n, earnings, cost, e); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function that calculates the profit ` `// with the earning and cost of expenses ` `static` `void` `calculateProfit(` `int` `n, ` `int` `[]earnings, ` ` ` `int` `[]cost, ` `int` `e) ` `{ ` ` ` ` ` `// To store the total Profit ` ` ` `int` `profit = ` `0` `; ` ` ` ` ` `// Loop for n number of days ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `int` `earning_per_day = ` `0` `; ` ` ` `int` `daily_spent_food = ` `0` `; ` ` ` ` ` `// If last day, no need to buy food ` ` ` `if` `(i == (n - ` `1` `)) ` ` ` `{ ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `profit = profit + earning_per_day; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Else buy food to gain ` ` ` `// energy for next day ` ` ` `if` `(cost[i] < earnings[i]) ` ` ` `{ ` ` ` ` ` `// Update earning per day ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `daily_spent_food = cost[i] * e; ` ` ` ` ` `// Update profit with daily spent ` ` ` `profit = profit + earning_per_day - ` ` ` `daily_spent_food; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the profit ` ` ` `System.out.print(profit + ` `"\n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given days ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `// Given earnings ` ` ` `int` `earnings[] = { ` `1` `, ` `8` `, ` `6` `, ` `7` `}; ` ` ` ` ` `// Given cost ` ` ` `int` `cost[] = { ` `1` `, ` `3` `, ` `4` `, ` `1` `}; ` ` ` ` ` `// Given energy e ` ` ` `int` `e = ` `5` `; ` ` ` ` ` `// Function call ` ` ` `calculateProfit(n, earnings, cost, e); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function that calculates the profit ` `# with the earning and cost of expenses ` `def` `calculateProfit(n, earnings, cost, e): ` ` ` ` ` `# To store the total Profit ` ` ` `profit ` `=` `0` `; ` ` ` ` ` `# Loop for n number of days ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `earning_per_day ` `=` `0` `; ` ` ` `daily_spent_food ` `=` `0` `; ` ` ` ` ` `# If last day, no need to buy food ` ` ` `if` `(i ` `=` `=` `(n ` `-` `1` `)): ` ` ` `earning_per_day ` `=` `earnings[i] ` `*` `e; ` ` ` `profit ` `=` `profit ` `+` `earning_per_day; ` ` ` `break` `; ` ` ` ` ` `# Else buy food to gain ` ` ` `# energy for next day ` ` ` `if` `(cost[i] < earnings[i]): ` ` ` ` ` `# Update earning per day ` ` ` `earning_per_day ` `=` `earnings[i] ` `*` `e; ` ` ` `daily_spent_food ` `=` `cost[i] ` `*` `e; ` ` ` ` ` `# Update profit with daily spent ` ` ` `profit ` `=` `(profit ` `+` `earning_per_day ` `-` ` ` `daily_spent_food); ` ` ` ` ` `# Print the profit ` ` ` `print` `(profit); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given days ` ` ` `n ` `=` `4` `; ` ` ` ` ` `# Given earnings ` ` ` `earnings ` `=` `[ ` `1` `, ` `8` `, ` `6` `, ` `7` `]; ` ` ` ` ` `# Given cost ` ` ` `cost ` `=` `[ ` `1` `, ` `3` `, ` `4` `, ` `1` `]; ` ` ` ` ` `# Given energy e ` ` ` `e ` `=` `5` `; ` ` ` ` ` `# Function call ` ` ` `calculateProfit(n, earnings, cost, e); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function that calculates the profit ` `// with the earning and cost of expenses ` `static` `void` `calculateProfit(` `int` `n, ` `int` `[]earnings, ` ` ` `int` `[]cost, ` `int` `e) ` `{ ` ` ` ` ` `// To store the total Profit ` ` ` `int` `profit = 0; ` ` ` ` ` `// Loop for n number of days ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `int` `earning_per_day = 0; ` ` ` `int` `daily_spent_food = 0; ` ` ` ` ` `// If last day, no need to buy food ` ` ` `if` `(i == (n - 1)) ` ` ` `{ ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `profit = profit + earning_per_day; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Else buy food to gain ` ` ` `// energy for next day ` ` ` `if` `(cost[i] < earnings[i]) ` ` ` `{ ` ` ` ` ` `// Update earning per day ` ` ` `earning_per_day = earnings[i] * e; ` ` ` `daily_spent_food = cost[i] * e; ` ` ` ` ` `// Update profit with daily spent ` ` ` `profit = profit + earning_per_day - ` ` ` `daily_spent_food; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the profit ` ` ` `Console.Write(profit + ` `"\n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given days ` ` ` `int` `n = 4; ` ` ` ` ` `// Given earnings ` ` ` `int` `[]earnings = { 1, 8, 6, 7 }; ` ` ` ` ` `// Given cost ` ` ` `int` `[]cost = { 1, 3, 4, 1 }; ` ` ` ` ` `// Given energy e ` ` ` `int` `e = 5; ` ` ` ` ` `// Function call ` ` ` `calculateProfit(n, earnings, cost, e); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

70

**Time Complexity:** O(N), where N is the number of days**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nuts & Bolts Problem (Lock & Key problem) | Set 1
- Secretary Problem (A Optimal Stopping Problem)
- Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
- Nuts & Bolts Problem (Lock & Key problem) | Set 2 (Hashmap)
- Applications of Minimum Spanning Tree Problem
- The Knight's tour problem | Backtracking-1
- Josephus problem | Set 1 (A O(n) Solution)
- Activity Selection Problem | Greedy Algo-1
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
- Frobenius coin problem
- Bin Packing Problem (Minimize number of used Bins)
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- The Lazy Caterer's Problem
- Chocolate Distribution Problem
- Max Flow Problem Introduction
- Gold Mine Problem
- Perfect Sum Problem (Print all subsets with given sum)
- Josephus Problem Using Bit Magic

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.