PROBLEM LINK:
Author: Aditya Chandran
Tester: Aishwarya
Editorialist: Aishwarya
DIFFICULTY:
CAKEWALK.
PREREQUISITES:
Greedy, Math.
PROBLEM:
You are given an array of size 3, having weights of vegetables needed.
Next n lines are arrays prices per kilo of the respective vegetables.
Find minimum cost required to but the vegetables.
EXPLANATION:
Simply multiplying the prices of the vegetables to the weight required and adding this cost for all vegetables will give you money required to be sent in that particular market.
Whichever market gives you the minimum cost, is the correct answer.
cost = \min_{k=i}^{n}(cost,(w1*p_i1 + w2*p_i2 + w3*p_i3))
TIME COMPLEXITY:
O(N).
SOLUTIONS:
Setter's Solution
import math
n=int(input())
w1,w2,w3=list(map(int,input().split()))
c=1e18
for i in range(n):
p1,p2,p3=list(map(int,input().split()))
cost=p1*w1+p2*w2+p3*w3
c=min(c,cost)
print(c)
Tester's Solution
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n;
cin >> n;
int w1,w2,w3;
cin >> w1 >> w2 >> w3;
int cost = INT_MAX;
for(int i=0;i<n;i++)
{
int p1,p2,p3;
cin >> p1 >> p2 >>p3;
cost = min(cost,(w1*p1 + w2*p2 + w3*p3));
}
cout << cost << endl;
return 0;
}