Origin of the heavy elements in binary neutron-star mergers from a gravitational-wave event