Delivery Man | CodeChef Solution

Hello coders, today we are going to solve Delivery Man CodeChef Solution whose Problem Code is TADELIVE.

Delivery Man

Contents

Task

Andy and Bob are the only two delivery men of Pizza-chef store. Today, the store received N orders. It’s known that the amount of tips may be different when handled by different delivery man. More specifically, if Andy takes the ith order, he would be tipped Ai dollars and if Bob takes this order, the tip would be Bi dollars.

They decided that they would distribute the orders among themselves to maximize the total tip money. One order will be handled by only one person. Also, due to time constraints Andy cannot take more than X orders and Bob cannot take more than Y orders. It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Andy or Bob.

Please find out the maximum possible amount of total tip money after processing all the orders.

Input Format

  • The first line contains three integers NXY.
  • The second line contains N integers. The ith integer represents Ai.
  • The third line contains N integers. The ith integer represents Bi.

Output Format

Print a single integer representing the maximum tip money they would receive.

Constraints

All test:

  • 1 ≤ N ≤ 105
  • 1 ≤ XY ≤ NX + Y ≥ N
  • 1 ≤ AiBi ≤ 104

10 points:

  • 1 ≤ N ≤ 20

30 points:

  • 1 ≤ N ≤ 5000

60 points:

  • 1 ≤ N ≤ 105

Example

Sample Input

5 3 3
1 2 3 4 5
5 4 3 2 1

Sample Output

21

Explanation

Bob will take the first three orders (or the first two) and Andy will take the rest (of course).

Solution – Delivery Man

C++

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, x, y, i;
    long long int tip = 0;
    int arr1[101000], arr2[101000];

    scanf("%d%d%d", &n, &x, &y);
    for(i = 0; i<n; i++) {
        scanf("%d", &arr1[i]);
    }
    for(i = 0; i<n; i++) {
        scanf("%d", &arr2[i]);
    }
    for(i = 0; i < n; i++) {
        arr2[i] = arr2[i] - arr1[i];
    }
    sort(arr2, arr2+n);

    for(i = 0; i < n; i++) {
        if(((y>0)&&(arr2[i]>0)) || (x==0)) {
            tip += arr1[i] + arr2[i];
            y--;
        }
        else {
            tip += arr1[i];
            x--;
        }
    }

    printf("%lld\n", tip);

    return 0;
}

Python

n,x,y=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ab=[]
for i in range(n):
	ab.append((abs(a[i]-b[i]),i))
ab.sort()
ab.reverse()
s=0
for j in range(n):
	k=ab[j][1]
	if a[k]>b[k] and x>0:
		s=s+a[k]
		x=x-1
	elif a[k]>b[k] and x==0:
		s=s+b[k]
	elif b[k]>a[k] and y>0:
		s=s+b[k]
		y=y-1
	else:
		s=s+a[k]
print(s)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class Tips implements Comparable<Tips>{
    private int andy;
    private int bob;
    private int diff;

    Tips(int andy, int bob, int diff) {
        this.andy = andy;
        this.bob = bob;
        this.diff = diff;
    }

    public int getAndy() {
        return this.andy;
    }

    public int getBob() {
        return this.bob;
    }

    public int getDiff() {
        return this.diff;
    }

    public int compareTo(Tips tip) {
        return tip.diff - this.diff;
    }
}

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();

        List<Tips> tips = new ArrayList<>();

        int[] andy = new int[n];
        for(int i=0; i<n; i++)
            andy[i] = sc.nextInt();

        int[] bob = new int[n];
        for(int i=0; i<n; i++) {
            bob[i] = sc.nextInt();
            tips.add(new Tips(andy[i], bob[i], Math.abs(andy[i]-bob[i])));
        }

        Collections.sort(tips);

        int total = 0;
        Iterator<Tips> itr = tips.iterator();

        while(itr.hasNext()) {
            Tips tip = itr.next();
            if(tip.getAndy() > tip.getBob() && x>0) {
                total += tip.getAndy();
                x--;
            }
            else {
                total += tip.getBob();
                y--;
            }
        }
    
        System.out.println(total);

    }
}

Disclaimer: The above Problem (Delivery Man) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.