Official

B - World Meeting Editorial by en_translator


First, prepare an array \(\mathrm{cnt}\) of length \(24\) as:

\[\mathrm{cnt}[j]=(\text{The number of employees belonging to a base with }X_i=j.\]

We exhaustively enumerate the start time of the meeting in Coordinated Universal Time (UTC). There are \(24\) choices: 0:00, 1:00, …, 23:00. (We can prove that starting the meeting at “not-o’clock” time is not beneficial.)

Fix the start time of the meeting at \(t\) o’clock UTC. Then, an employee at base \(i\) can attend the meeting if and only if \(9 \leq (t+X_i)\bmod 24 < 18\). This is equivalent to \((9-t) \bmod 24 \leq X_i < (18-t)\bmod 24\), so the number of employees that can attend this meeting can be found as \(\mathrm{cnt}[(9-t) \bmod 24] + \mathrm{cnt}[(10-t) \bmod 24] +\dots + \mathrm{cnt}[(17-t) \bmod 24] \).

All that left is to count the employees who can attend the meeting for all \(24\) cases, and print the maximum among them.

One can also show that the correct answer can be simply obtained as the maximum value of \(\mathrm{cnt}[i \bmod 24] + \mathrm{cnt}[(i+1) \bmod 24] +\dots + \mathrm{cnt}[(i+8) \bmod 24] \) among \(i=0,1,...,23\). The sample code below adopts this approach.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> cnt(24);
    for (int i = 0; i < n; i++) {
        int w, x;
        cin >> w >> x;
        cnt[x] += w;
    }
    int ans = 0;
    for (int i = 0; i < 24; i++) {
        int sum = 0;
        for (int j = 0; j < 9; j++) {
            sum += cnt[(i + j) % 24];
        }
        ans = max(ans, sum);
    }
    cout << ans << endl;
}

Sample code (Python) :

n = int(input())
cnt = [0 for _ in range(24)]
for i in range(0, n):
    w, x = map(int, input().split())
    cnt[x] += w

ans = 0
for i in range(24):
    sum = 0
    for j in range(9):
        sum += cnt[(i + j) % 24]
    ans = max(ans, sum)
    
print(ans)

posted:
last update: