The world's largest toy maker agreed to buy Canada's Mega Brands Inc. for 407.5 million Canadian dollars ($366.4 million) to get a toehold in the construction toy market dominated by Denmark's Lego A/S, at a time when Mattel's Barbie, Hot Wheels and Fisher-Price brands are struggling.