Official

A - Robot Balance Editorial by en_translator


For beginners

This problem can be solved by implementing the following process:

  • If the head weight is less than or equal to body weight, print \(0\).
  • Otherwise, print \(H-B\).

This process can be implemented by using an if statement, or using an max statement after applying a slight deformation on the expressions.

The following is sample code.

Sample code using if statement

#include <iostream>
using namespace std;

int main() {
    int H, B;
    cin >> H >> B;

    if (H <= B) { // If head weight is less than or equal to body weight,
        cout << 0 << endl; // print 0
    } else { // Otherwise,
        cout << H - B << endl; // print H - B
    }
    return 0;
}
H, B = map(int, input().split())

if H <= B: # If head weight is less than or equal to body weight,
    print(0) # print 0
else: # Otherwise,
    print(H - B) # print H - B

Sample code using max function

The process above can be rephrased as:

  • Print \(B-B\) if \(H\le B\).
  • Print \(H-B\) otherwise.

In other words, we are asked to:

  • print \(\max\lbrace H,B\rbrace-B\).

The problem can be solved by implementing this.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int H, B;
    cin >> H >> B;

    cout << max(H, B) - B << endl;
    return 0;
}
H, B = map(int, input().split())

print(max(H, B) - B)

posted:
last update: