So the first thing that you're missing is that those are two different systems; one has a flywheel attached to the weight; the other apparently does not have the flywheel attached to anything in particular.
The second thing you're missing is that, if it's connected, then the string is capable of transferring energy between the two systems over long timescales. The weight is slowly getting kicked up by random thermal kicks; this usually creates slack in the rope, but on some occasions a random thermal kick on the flywheel will happen to draw the rope taut at the same time, and as the weight gets kicked back down it pulls the flywheel forward by more than the random kick over in B moved it back. It is a very slow transfer of energy, but it is in fact there.
A similar thought experiment is Feynman's ratchet. Feynman's thought experiment is to connect the random bumps of a system to a ratchet, so the ratchet only turns one way with the random bumps, and thus you get a perpetual motion machine. The reason that it doesn't work involves peeking inside the ratchet, at which point we see a little spring pressing a little bar into an asymmetrically toothed surface so that when you're pushing along the gradual slope of the teeth the force requirements to change the ratchet's angle are low, but when you're pushing the other way the requirements are high. He invites you to think about what needs to happen for the bar to thermally get kicked to the height of the teeth: it requires the spring to be thermalized so that it bounces up and down to the same level! Well, if it does that, then the fundamental mechanism of asymmetry in the ratchet becomes unimportant at thermal energy scales, and the thing stops working. It only works because you're transferring energy from a hot reservoir (system) to a cold reservoir (spring)... and you weren't expecting that because in your experience this energy transfer is very small and you thought "I can always ignore that effect", but in many circumstances the laws of thermodynamics do not ignore those effects, even if we do.