When you heat the metal, it gained thermal energy and expanded. Metal B has greater expansivity than A so it expanded more than A. When it expands, it's molecules moved further apart so it became bigger. When there is no place for B to expand, it bent upwards. When it is cooled, it loses thermal energy, it's molecules vibrate slower, the molecules nearer to each other and contracts.