PROBLEM LINK:
Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1
Author: Souradeep
Tester : Riley Borgard
Editorialist: Aman Dwivedi
DIFFICULTY:
Cakewalk
PREREQUISITES:
Greedy
PROBLEM:
Chef has three spells. Their powers are A, B, and C respectively. Initially, Chef has 0 hit points, and if Chef uses a spell with power P, then his number of hit points increases by P.
Before going to sleep, Chef wants to use exactly two spells out of these three. Find the maximum number of hit points Chef can have after using the spells.
EXPLANATION:
Since our goal is to maximize the number of hit points, it is optimal to use those spells with maximum powers. As we need to take exactly two spells, just take the maximum and second maximum power spell.
In another way, our answer is just a maximum of [A+B, B+C, C+A]
TIME COMPLEXITY:
O(1) per test case
SOLUTIONS:
Author
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=1;
t=sc.nextInt();
//int t=Integer.parseInt(br.readLine());
while(--t>=0){
int x=sc.nextInt();
int y=sc.nextInt();
int z=sc.nextInt();
int g=Math.max(Math.max(x+y,y+z),x+z);
System.out.println(g);
}
}
}
Tester
#include <bits/stdc++.h>
#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int te;
cin >> te;
while(te--) {
int a, b, c;
cin >> a >> b >> c;
cout << max({a + b, b + c, c + a}) << '\n';
}
}
Editorialist
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int a[3];
cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
cout<<(a[2]+a[1])<<"\n";
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin>>t;
while(t--)
solve();
return 0;
}