Editorial for Midterm Madness
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Parsa has files for each unit in his course, along with one merged file which was created for his midterm exam (consisting of
files where
), and another merged file which was created for his final exam, which combines the
notes for all units in his course. This means that you are provided with
files in the input.
What you have to do is to find both of the merged files that were used for his midterm exam and final exam.
Finding the final exam merged file is quite straightforward, as it is the largest file in the collection of files. Now that we know the final exam merged file, we also know the sum of the
files which were created for each unit in Parsa's coursework. Given that we know the sum of the
files and the final exam merged file, this allows us to find the midterm exam merged file.
If the largest file size is , we can take the sum of all file sizes then subtract
, (accounting for each of the
file sizes as well as the final exam merged file) to find the size of the midterm exam merged file.
When implementing this, for each file size that you read in, you can use a variable to keep track of the current largest file, then have a variable to store the running sum of the total file size. You can then output the answer by using the calculation stated above.
You can calculate the sum and the largest value while taking in the file sizes, making the optimal solution
. While it is possible to sort the file sizes to find the maximum value, this is not necessary. We can optimally use
space, as we don't necessarily need to store inputs, although its totally fine to just store the inputs for
space too.
C++ solution:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
n += 2;
ll largest = 0;
ll sum = 0;
while (n--) {
ll a;
cin >> a;
largest = max(largest, a);
sum += a;
}
cout << (sum - largest * 2) << " " << largest << endl;
}
Python solution:
input()
els = list(map(int, input().split()))
print(sum(els) - 2 * max(els), max(els))
Comments