Language: C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

//============================================================================
#define pred(x) ((x)-1)
#define succ(x) ((x)+1)
//============================================================================
typedef struct{
    long ary;
    long num;
}TCarSet;
//============================================================================
//============================================================================

//============================================================================
//============================================================================
//long weight(unsigned short *ary, long *i, long *max)
//============================================================================
/*
    1<= wi <=10000
    1<= n <= 100000
    1<= k <= 100000
*/
//----------------------------------------------------------------------------
long sumcar(TCarSet *car, unsigned short *ary)
{
    long i,j,sum;

    for (i=sum=0, j=car->ary; i<car->num; i++, j++)
        sum+=ary[j];
    return sum;
}
//----------------------------------------------------------------------------
void carinc(TCarSet *dst, TCarSet *src)
{
    dst->num++, src->num--, src->ary++;
}
//----------------------------------------------------------------------------
void cardec(TCarSet *dst, TCarSet *src)
{
    dst->num--, src->num++, src->ary--;
}
//----------------------------------------------------------------------------
long caradjust(TCarSet *dst, TCarSet *src, long ave, unsigned short *ary)
{
    long dval, sval;

    dval = sumcar(dst, ary);
    if (dval==ave)
        return dval;
    while(dval<ave && 1<src->num){
        carinc(dst, src);
        sval=dval = sumcar(dst, ary);
    }
    while(sval>ave && 1<dst->num){
        cardec(dst, src);
        sval = sumcar(dst, ary);
    }
    if (abs(sval-ave)<abs(dval-ave))
        return sval;
    carinc(dst, src);
    return dval;
}
//----------------------------------------------------------------------------
void carshift(TCarSet *car, long max, long min)
{
    void (*fptr)(TCarSet *, TCarSet *);
    long i,f,e;

    if (max<min){
        fptr=cardec;
        f=max, e=min;
    }else{
        fptr=carinc;
        f=min, e=max;
    }
    for (i=f; i<e; i++)
        fptr(&car[i], &car[succ(i)]);
}
//----------------------------------------------------------------------------
void input(void)
{
    long i,K,N,n,ave,max,sum,val;
    unsigned short *ary;

    scanf("%ld", &N);
    scanf("%ld", &K);
    ary=malloc(N*sizeof(unsigned short));
    for (i=sum=max=0; i<N; i++){
        scanf("%d", &ary[i]);
        sum+=ary[i];
    }
    ave = sum/K;
    if (1<K){
        for (max=ave; max<sum; max++){
            //printf("max=%ld:", max);
            for (i=n=0; i<K && n<N; i++){
                for (val=0; val<=max && n<N; n++)
                    val+=ary[n];
                if (max<val){
                    n--;
                    val-=ary[n];
                }
                //printf(" %ld", val);
                if (n==N && i<K)
                    continue;
            }
            //printf("\n");
            if (n==N && i==K && val<=max)
                break;
        }
    }
    printf("%ld\n", max);
    free(ary);
}
//============================================================================
#ifndef DESKTOP
int main()
{
    input();
    return 0;
}
#endif

Compiler output:
a.cc: In function 'void input()':
a.cc:91:15: error: invalid conversion from 'void*' to 'short unsigned int*' [-fpermissive]
   91 |     ary=malloc(N*sizeof(unsigned short));
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
      |               |
      |               void*