Related Articles
• Difficulty Level : Medium
• Last Updated : 07 May, 2021

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.

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 = 5
Output: 20
Explanation:
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=5
Output: Total Profit = 0 + 10 = 10
Explanation:
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:

1. For each element in earnings[] and cost[], compare earning[i] with cost[i].
2. 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.
3. 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.
4. 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.
5. Print the total profit after the above steps.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``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);``}`

## 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`

## 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`

## 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`

## Javascript

 ``
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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up