Need Help on USACO Bronze "Candy Cane Feast"

Problem Info

Candy Cane Feast - USACO Bronze


I was wondering if someone could help me debug this problem, since my code only works for the test case but nothing else.

What I’ve Tried

I have tried debugging the code via printing throughout the loops and fixing issues, and I have completed doing that, and I was able to get the test case, however it seems to not work for anything else other than the sample test case.

My Work

#include <iostream>
#include <cmath>

using namespace std;

int main(){
  int n, m;
  cin >> n >> m;

  int cows[n];
  for(int i = 0; i < n; i++){
    cin >> cows[i];

  int canes[m];
  for(int i = 0; i < m; i++){
    cin >> canes[i];

  for(int i = 0; i < 2; i++){
  int offtheground = 0;
    for(int j = 0; j < n; j++){
      int temp = 0;
      if(cows[j] > offtheground){
        temp = abs(canes[i] - cows[j]);
        if(temp < canes[i]){
        canes[i] = canes[i] - temp;
        cows[j] = cows[j] + temp;
        offtheground = temp;

          cows[j] = cows[j] + canes[i];
      else if(canes[i] <= 0){
  for(int k = 0; k < n; k++){
    cout << cows[k] << endl;

In this, I used an offtheground variable in order to check when a cow is tall enough to reach the candy cane, and I used a temp variable to subtract from the cane height, and add it to the cow height when the cow is able to reach the candy cane.