Why does it take millions of dollars to run for president?

It costs so much to run for president because whoever advertises better is elected. Just like how people buy products over another. Nobody would have voted for Obama if the first time they saw his name was on the ballot.