Editorial for Midterm Madness


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: ray

Parsa has n files for each unit in his course, along with one merged file which was created for his midterm exam (consisting of m files where 1 \leq m \leq n -1), and another merged file which was created for his final exam, which combines the n notes for all units in his course. This means that you are provided with n+2 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 n+2 files. Now that we know the final exam merged file, we also know the sum of the n files which were created for each unit in Parsa's coursework. Given that we know the sum of the n files and the final exam merged file, this allows us to find the midterm exam merged file.

If the largest file size is x, we can take the sum of all file sizes then subtract 2x, (accounting for each of the n 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 n+2 file sizes, making the optimal solution O(n). While it is possible to sort the file sizes to find the maximum value, this is not necessary. We can optimally use O(1) space, as we don't necessarily need to store inputs, although its totally fine to just store the inputs for O(n) 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

There are no comments at the moment.