This cost is directly related to licensing each movie.
Each movie has a contract to license the movie ("terms"), which are negotiated with the exhibitor/theater on a per movie basis. The contract will specify a certain percentage of box office ticket sales which are due to the film distributor. There will also typically be a minimum charge of a few hundred dollars (to cover the production of the copy of the movie and the delivery of it to the theater) that will be owed if the amount owed from the percentage doesn't meet that threshold. It is quite rare that these actually get charged as most theaters will sell enough tickets to owe well over the minimum charge.
A typical set of terms will say that the movie distributor is owed anywhere from 50 to 75% of the ticket sale price for first-run, new release Hollywood movies. Some movies will have a scale where the percentage price goes down over a number of weeks the longer the theater keeps the movie showing. Other movies will have a scale which sets a fixed percentage over all weeks which the theater shows the movie, but that percentage is determined by how much the movie makes in total at the box office nationwide of worldwide (ie. the more the movie makes in total, the percentage which will be owed goes up). For an older release (more then a month old), or some small "Mom & Pop movie" 30-40% is typical.
The theaters are required to provide a record of ticket sales to the movie company, most of which are done electronically through the theaters ticketing system, without any human intervention at the top of the hour every hour.
At the end of each week (a week usually runs from Friday to Thursday), the theater and movie company will double check their numbers, and then a payment will be issued (usually via wire transfer, else by check) to the movie distributors bank account at some point in the following couple of weeks.
So in short, the movies are never paid for up front, they get their payment in the days and weeks after the actual show.
The one exception to this, where either a small theatre is just starting in business, or were some theater/chain has a long record of not actually paying their bills in a timely manner, they may be asked for an "advance", where they are required to pay a sum of money up front in order to obtain a license to show the film (and a copy of the film itself). This lump sum will then be used as a payment towards whatever amount owed (calculated by the percentage described above) during settlement after the week(s) are over.