"Discretionary" is generally defined as "dependent on a decision". Sometimes the use of the term instead of "optional" infers an "either-or" decision (you may do one thing, or you may do another, but you must do something), or a moral choice, or a choice to abstain from something (the connotation being that humans have "discretion", the power to make decisions that affect other people, unlike animals who do what they feel like doing regardless of the effect on their surroundings).
In your provided example, you can choose to consume candy, alcohol, or additional food from the main categories, in any combination up to a certain number of calories, or not at all. It's your choice, and thus the calories involved are dependent on your decision to consume them or not, and in what form.
The noun "discretion" from which we get the adjective "discretionary" is rooted in Latin (discretio - "discernment, power to make decisions") and probably came to English either through Latin or Old French. The first known use of the term "at [one's] discretion" using the present spelling and understood meaning is from 1570, though prior spellings and meanings (mainly toward the moral or abstaining connotation) existed in English as early as the 1300s.
EDIT FROM COMMENTS: I got my information from a very useful resource, Online Etymology Dictionary.
As far as other examples, "discretionary" is often used in the context of money, such as "discretionary spending" (you'll hear that term in terms of government budgets). Money that is "discretionary" is not already budgeted and required to be spent on prior commitments or on known expenses; therefore, you have a choice as to what to do with it.
Things that are not considered discretionary in most industrial nations' households including the U.S. include your mortgage or rent, loan payments like for a car or student loans, your utility bills, food and gas. Things that ARE generally considered discretionary include entertainment, clothing (yes, clothing is required, but you choose what to buy and how much to spend), home decor, electronics, etc.
For the U.S. government, non-discretionary spending generally falls into five categories; interest on the national debt, Social Security, health benefits (Medicare/Medicaid), income security (federal unemployment, welfare and retirement benefits other than SocSec), and Veteran's Affairs (military pensions, disability and healthcare). Those five areas of government spending comprise the overwhelming majority of the U.S. federal budget, and are pretty much untouchable for a variety of legal and political reasons (changing how much is spent for the programs requires changing the laws that define the payments under those programs, which the people who are affected by those changes will always fight to prevent). Pretty much everything else our taxes pay for, including roads, airports, infrastructure, research grants, our currently-serving military, etc is "discretionary". Like a household budget, these areas of spending are the minority of the budget, and still include things we "need".