Peanut Packing

A man is running a super market and he observes the pattern of customer buying peanuts in packs of 1 kg, 2 kgs, 3 kgs till N kgs. So based on the buying pattern and his observation he can price the packs of 1 kg, 2 kgs, … N kgs  at rupees P(1), P(2), P(3), … P(N) respectively. What is the maximum revenue he can earn when selling N kgs of peanuts based on the given pricing?

Input Format:
The first line contains N.
The second line contains the integer value denoting the price for 1 kg pack, 2 kgs pack till N kgs pack with each value separated by a space.

Output Format:
The first line contains the maximum revenue he can earn by packing and selling the N kgs peanuts based on the given pricing.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
4
120 250 360 490

Output:
500

Explanation:
While selling 4 kgs of peanuts, the maximum revenue of Rs.500 is obtained when he packs the 4 kgs as 2 kgs + 2kgs and sells them at Rs. 250 each.

Example Input/Output 2:
Input:
4
120 240 360 490

Output:
490

Explanation:
While selling 4 kgs of peanuts, the maximum revenue of Rs.490 is obtained when he packs the 4 kgs as a single 4 kgs pack and sells it ar Rs.490

def calculate_max_revenue(n, prices):
    # Initialize max_revenue to store the maximum revenue
    max_revenue = 0

    # Iterate from 1 to N (inclusive)
    for i in range(1, n + 1):
        # Check if N is divisible by i
        if n % i == 0:
            # Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
            s = prices[i] * (n // i)
            if max_revenue < s:
                max_revenue = s
        else:
            # Calculate the revenue for selling N kgs as (N // i) kgs packs and the remaining (N % i) kgs pack
            s = prices[i] * (n // i) + prices[n % i]
            # Update max_revenue if it's greater
            if max_revenue < s:
                max_revenue = s

    return max_revenue

n = int(input())
prices = list(map(int, input().split()))
result = calculate_max_revenue(n, prices)
print(result)
#include <iostream>
using namespace std;

int calculate_max_revenue(int n, int prices[]) {
    // Initialize max_revenue to store the maximum revenue
    int max_revenue = 0;

    // Iterate from 1 to N (inclusive)
    for (int i = 1; i <= n; i++) {
        // Check if N is divisible by i
        if (n % i == 0) {
            // Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
            int s = prices[i] * (n / i);
            if (max_revenue < s) {
                max_revenue = s;
            }
        } else {
            // Calculate the revenue for selling N kgs as (N / i) kgs packs and the remaining (N % i) kgs pack
            int s = prices[i] * (n / i) + prices[n % i];
            // Update max_revenue if it's greater
            if (max_revenue < s) {
                max_revenue = s;
            }
        }
    }

    return max_revenue;
}

int main() {
    int n;
    cin >> n;
    int prices[n + 1];

    for (int i = 1; i <= n; i++) {
        cin >> prices[i];
    }

    int result = calculate_max_revenue(n, prices);
    cout << result;
    return 0;
}
#include <stdio.h>

int calculate_max_revenue(int n, int prices[]) {
    // Initialize max_revenue to store the maximum revenue
    int max_revenue = 0;

    // Iterate from 1 to N (inclusive)
    for (int i = 1; i <= n; i++) {
        // Check if N is divisible by i
        if (n % i == 0) {
            // Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
            int s = prices[i] * (n / i);
            if (max_revenue < s) {
                max_revenue = s;
            }
        } else {
            // Calculate the revenue for selling N kgs as (N / i) kgs packs and the remaining (N % i) kgs pack
            int s = prices[i] * (n / i) + prices[n % i];
            // Update max_revenue if it's greater
            if (max_revenue < s) {
                max_revenue = s;
            }
        }
    }

    return max_revenue;
}

int main() {
    int n;
    scanf("%d", &n);
    int prices[n + 1];

    for (int i = 1; i <= n; i++) {
        scanf("%d", &prices[i]);
    }

    int result = calculate_max_revenue(n, prices);
    printf("%d", result);
    return 0;
}
using System;

public class Program {
    public static int CalculateMaxRevenue(int n, int[] prices) {
        // Initialize max_revenue to store the maximum revenue
        int max_revenue = 0;

        // Iterate from 1 to N (inclusive)
        for (int i = 1; i <= n; i++) {
            // Check if N is divisible by i
            if (n % i == 0) {
                // Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
                int s = prices[i] * (n / i);
                if (max_revenue < s) {
                    max_revenue = s;
                }
            } else {
                // Calculate the revenue for selling N kgs as (N / i) kgs packs and the remaining (N % i) kgs pack
                int s = prices[i] * (n / i) + prices[n % i];
                // Update max_revenue if it's greater
                if (max_revenue < s) {
                    max_revenue = s;
                }
            }
        }

        return max_revenue;
    }

    public static void Main() {
        int n = Convert.ToInt32(Console.ReadLine());
        string[] prices_str = Console.ReadLine().Split(' ');
        int[] prices = new int[n + 1];

        for (int i = 1; i <= n; i++) {
            prices[i] = Convert.ToInt32(prices_str[i - 1]);
        }

        int result = CalculateMaxRevenue(n, prices);
        Console.Write(result);
    }
}
import java.util.*;

public class Main {
    public static int calculateMaxRevenue(int n, int[] prices) {
        // Initialize max_revenue to store the maximum revenue
        int max_revenue = 0;

        // Iterate from 1 to N (inclusive)
        for (int i = 1; i <= n; i++) {
            // Check if N is divisible by i
            if (n % i == 0) {
                // Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
                int s = prices[i] * (n / i);
                if (max_revenue < s) {
                    max_revenue = s;
                }
            } else {
                // Calculate the revenue for selling N kgs as (N / i) kgs packs and the remaining (N % i) kgs pack
                int s = prices[i] * (n / i) + prices[n % i];
                // Update max_revenue if it's greater
                if (max_revenue < s) {
                    max_revenue = s;
                }
            }
        }

        return max_revenue;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] prices = new int[n + 1];

        for (int i = 1; i <= n; i++) {
            prices[i] = scan.nextInt();
        }

        int result = calculateMaxRevenue(n, prices);
        System.out.print(result);
        scan.close();
    }
}
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function calculateMaxRevenue(n, prices) {
    // Initialize max_revenue to store the maximum revenue
    let max_revenue = 0;

    // Iterate from 1 to N (inclusive)
    for (let i = 1; i <= n; i++) {
        // Check if N is divisible by i
        if (n % i === 0) {
            // Calculate the revenue for selling N kgs as i kgs packs and update max_revenue if it's greater
            let s = prices[i] * (n / i);
            if (max_revenue < s) {
                max_revenue = s;
            }
        } else {
            // Calculate the revenue for selling N kgs as (N / i) kgs packs and the remaining (N % i) kgs pack
            let s = prices[i] * Math.floor(n / i) + prices[n % i];
            // Update max_revenue if it's greater
            if (max_revenue < s) {
                max_revenue = s;
            }
        }
    }

    return max_revenue;
}

function main() {
    rl.question("Enter N: ", n_input => {
        const n = parseInt(n_input);
        rl.question("Enter prices separated by space: ", prices_input => {
            const prices = prices_input.split(' ').map(Number);
            const result = calculateMaxRevenue(n, prices);
            console.log(result);
            rl.close();
        });
    });
}

main();
Previous Article

HCF (GCD) of long values

Next Article

Sort By Last Name

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *