---
# System prepended metadata

title: 50246. Minimum sum of squares
tags: [C Programming, recursion, judgegirl]

---

```clike
#include<stdio.h>
#include<stdint.h>
#define N 25
#define K 15
// #define debug
uint64_t min(uint64_t a, uint64_t b){
    return(a * (a < b) + b * (a >= b));
}
void solve(int arr[], int idx, int n,
uint64_t subsets[], int k, uint64_t nowsum, uint64_t *minsum){
    if(idx >= n || nowsum >= *minsum){
        *minsum = min(nowsum, *minsum);
        #ifdef debug
        printf("now minsum = %lu\n\n", *minsum);
        #endif
        return;
    }
    for(int i = 0; i < k; i++){
        uint64_t now = nowsum - subsets[i] * subsets[i];
        subsets[i] += arr[idx];
        now += subsets[i] * subsets[i];
        #ifdef debug
        printf("put arr[%d] = %d into %dth subset, nowsum = %lu\n", idx, arr[idx], i, now);
        #endif
        solve(arr, idx + 1, n, subsets, k, now, minsum);
        subsets[i] -= arr[idx];
        #ifdef debug
        printf("remove arr[%d] = %d from %dth subset\n", idx, arr[idx], i);
        #endif
        if(!subsets[i])
            return;
    }
}
int main(){
    int n, k;
    scanf("%d%d", &n, &k);
    int arr[N];
    for(int i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    uint64_t subsets[K] = {0};
    uint64_t minsum = UINT64_MAX;
    solve(arr, 0, n, subsets, k, (uint64_t)0, &minsum);
    printf("%lu\n", minsum);
}
```