OK, just to be clear, we’re not green-lighting a king-size Snickers habit. But it’s true sugar and exercise go together like chocolate and peanuts. “These days, there’s such a focus on protein and healthy fats and avoiding carbs at all costs (including sugar, which is, of course, a carb). However, carbs are actually good—and necessary!—if you exercise regularly, particularly for endurance workouts,” says Melissa Majumdar, R.D., a spokesperson for the Academy of Nutrition and Dietetics who specializes in sports nutrition.American College of Sports Medicine position stand. Nutrition and athletic performance., , . Medicine and science in sports and exercise, 2009, May.;41(3):1530-0315.

Here’s a quick primer: Whether you’re eating a complex carb (like brown rice) or a simple one (the honey on your Greek yogurt), your body breaks it down into glucose—and that liquid sugar coursing through your veins is the preferred fuel source for your muscles (and brain, and everything else for that matter). “Think of it as the energy that powers the building,” Majumdar says. “Your body can use fat and protein as fuel, but they’re not nearly as efficient and readily available as carbohydrates.”

The thing is, no matter how much linguine you inhale before a HIIT session or a 10K run, your body can only store so much. And during long, challenging workouts—we’re talking more than 60 minutes—your body will burn through its glycogen stores. If you don’t replenish them, you won’t have the energy to keep going. Read: bonk! (For shorter cardio sessions and resistance workouts, which don’t rip through as much glucose, extra carbs aren’t necessary, Majumdar notes.)

Being carb depleted also impedes muscle repair and recovery—not exactly the situation you want. And while, sure, you could grab any old bar or sports drink to get your sugar fix, to really do it right, there’s actually an art to it. Here’s exactly how much you need and what to know about timing your carb intake.

Your Daily Carb Requirements

To just maintain the status quo—meaning, a day you’re not exerting yourself too much—you need about 3 to 5 grams of carbohydrates per kilogram of body weight. But endurance workouts require extra: Once you hit that 60-minute mark, the number jumps to 5 to 7 grams per kilo of body weight.

To give you an idea what that looks like, for a 130-pound woman, it’s equivalent to eating a cup of steel-cut oatmeal, 2 slices of bread, 1 1/2 cups of whole-wheat pasta, a granola bar, and 1/2 cup of dried fruit over the course of the day. So, you know, totally doable.

What to Eat Before an Endurance Workout

“The ideal way to prep is to have a meal of complex carbs three to four hours before you exercise—something like whole-grain toast with peanut butter and honey, or oatmeal with skim milk, almonds, and sliced banana,” Majumdar says. “Whole grains stay with you, and the more complex the carb, the longer it will take your body to break it down.” But if you miss the boat and need to fuel up much closer to your workout—say, 30 to 60 minutes prior—then simple carbs are the way to go, because of the quick hit of energy they’ll deliver, she says. Think: a handful of pretzels, an energy bar, or toast with jam.

What to Have During Your Sweat Sesh

“After that hour mark, that’s when you’re going to want to reach for sports drinks, gels, and chews—about 30 to 60 grams worth. Again, it’s those simple carbs you want,” Majumdar says. And products like these make it into your bloodstream particularly quick. For perspective: A 20-ounce sports drink has around 36 grams of carbs.

But don’t go overboard or make yourself crazy trying to count carb grams. Just have a little something when you begin to feel like you’re flagging. Or if you’re training for a longer race like a 10K or half-marathon, practice your fuel plan (such as one energy chew every 15 to 20 minutes) during training. But, she adds, “do steer clear of fat and protein, because they’ll slow digestion and your body’s ability to use those carbs.”

How to Refuel Afterward

Majumdar recommends a four-to-one ratio of carbs to protein for post-workout recovery. “Avoid high-fat meals like fried foods or eating a whole avocado because it will interfere with the process,” she says. “Your body won’t be able to use the protein and carbs to replete glycogen and aid in muscle repair and recovery as well.” A smoothie made with Greek yogurt or some cottage cheese with whole-grain crackers are two good (and quick) options to reach for.

Just remember, sugar isn’t always the enemy, but it does taste a whole lot sweeter when you’ve earned it.