#define PROBLEM \
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B"
#include<bits/stdc++.h>#include"library/flow/MCF.hpp"intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn,m,f;std::cin>>n>>m>>f;MCF<int,int>fl(n);while(m--){intu,v,c,d;std::cin>>u>>v>>c>>d;fl.add_arc(u,v,c,d);}auto[ans,ok]=fl.flow(f);std::cout<<(ok?ans:-1)<<std::endl;}
#line 1 "test/AOJ/GRL_6_B.test.cpp"
#define PROBLEM \
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_B"
#include<bits/stdc++.h>#line 2 "library/graph/WeightedGraph.hpp"
template<typenameT>structWeightedEdge{WeightedEdge()=default;WeightedEdge(intfrom,intto,Tweight):from(from),to(to),weight(weight){}intfrom,to;Tweight;operatorint()const{returnto;}};template<typenameT>structWeightedGraph{intn;usingweight_type=T;usingedge_type=WeightedEdge<T>;std::vector<edge_type>edges;protected:std::vector<int>in_deg;boolprepared;classOutgoingEdges{WeightedGraph*g;intl,r;public:OutgoingEdges(WeightedGraph*g,intl,intr):g(g),l(l),r(r){}edge_type*begin(){return&(g->edges[l]);}edge_type*end(){return&(g->edges[r]);}edge_type&operator[](inti){returng->edges[l+i];}intsize()const{returnr-l;}};classConstOutgoingEdges{constWeightedGraph*g;intl,r;public:ConstOutgoingEdges(constWeightedGraph*g,intl,intr):g(g),l(l),r(r){}constedge_type*begin()const{return&(g->edges[l]);}constedge_type*end()const{return&(g->edges[r]);}constedge_type&operator[](inti)const{returng->edges[l+i];}intsize()const{returnr-l;}};public:OutgoingEdgesoperator[](intv){assert(prepared);return{this,in_deg[v],in_deg[v+1]};}constConstOutgoingEdgesoperator[](intv)const{assert(prepared);return{this,in_deg[v],in_deg[v+1]};}boolis_prepared()const{returnprepared;}WeightedGraph():n(0),in_deg(1,0),prepared(false){}WeightedGraph(intn):n(n),in_deg(n+1,0),prepared(false){}WeightedGraph(intn,intm,booldirected=false,intindexed=1):n(n),in_deg(n+1,0),prepared(false){scan(m,directed,indexed);}voidresize(intn){n=n;}voidadd_arc(intfrom,intto,Tweight){assert(!prepared);assert(0<=fromandfrom<nand0<=toandto<n);edges.emplace_back(from,to,weight);in_deg[from+1]++;}voidadd_edge(intu,intv,Tweight){add_arc(u,v,weight);add_arc(v,u,weight);}voidadd_arc(constedge_type&e){add_arc(e.from,e.to,e.weight);}voidadd_edge(constedge_type&e){add_edge(e.from,e.to,e.weight);}voidscan(intm,booldirected=false,intindexed=1){edges.reserve(directed?m:2*m);while(m--){intu,v;std::cin>>u>>v;u-=indexed;v-=indexed;Tweight;std::cin>>weight;if(directed)add_arc(u,v,weight);elseadd_edge(u,v,weight);}build();}voidbuild(){assert(!prepared);prepared=true;for(intv=0;v<n;v++)in_deg[v+1]+=in_deg[v];std::vector<edge_type>new_edges(in_deg.back());autocounter=in_deg;for(auto&&e:edges)new_edges[counter[e.from]++]=e;edges=new_edges;}voidgraph_debug()const{#ifndef __DEBUG
return;#endif
assert(prepared);for(intfrom=0;from<n;from++){std::cerr<<from<<";";for(inti=in_deg[from];i<in_deg[from+1];i++)std::cerr<<"("<<edges[i].to<<","<<edges[i].weight<<")";std::cerr<<"\n";}}};#line 3 "library/flow/MCF.hpp"
#define REP_(i, n) for (int i = 0; i < (n); i++)
template<typenameTF,typenameTC>classMCF{structEdgeInfo{TFcap;TCcost;intrev;};intn,s,t;WeightedGraph<EdgeInfo>G;std::vector<TC>potential,dist;staticconstexprTCINF=std::numeric_limits<TC>::max()/2;std::vector<std::pair<int,int>>pre,edge_memo;// pre[v]=[u,i] : G[u][i] で v に来たstd::vector<int>in_deg,out_deg;boolnegative,dag;template<typenameT>boolchmin(T&a,constT&b){return(a>band(a=b,true));}boolSP_update(intfrom,intedge_id){if(dist[from]==INF)returnfalse;constauto&e=G[from][edge_id];if((e.weight).cap==0)returnfalse;if(chmin(dist[e.to],dist[from]+(e.weight).cost+potential[from]-potential[e.to])){pre[e.to]={from,edge_id};returntrue;}returnfalse;}std::priority_queue<std::pair<TC,int>,std::vector<std::pair<TC,int>>,std::greater<std::pair<TC,int>>>que;voiddijkstra(){// dist[i]:sから残余グラフで辺の重みによるiへの最短路// となるようにdistを作るstd::ranges::fill(dist,INF);dist[s]=0;que.emplace(0,s);while(que.size()){constauto[now,v]=que.top();que.pop();if(dist[v]<now)continue;REP_(i,G[v].size())if(SP_update(v,i))que.emplace(dist[G[v][i].to],G[v][i].to);}}voidDAG(){negative=false;std::ranges::fill(dist,INF);dist[s]=0;std::queue<int>que;REP_(i,n)if(!in_deg[i])que.push(i);while(que.size()){intv=que.front();que.pop();REP_(i,G[v].size()){SP_update(v,i);if(!--in_deg[G[v][i].to])que.push(G[v][i].to);}}}voidBellmanFord(){negative=false;std::ranges::fill(dist,INF);dist[s]=0;REP_(_,n){boolupdate=false;REP_(v,n)if(dist[v]<INF)REP_(i,G[v].size())if(SP_update(v,i))update=true;if(!update)return;}assert(false);// 負閉路}public:MCF(){}MCF(intn_,ints_=0,intt_=-1):n(n_),G(n_),potential(n_,0),dist(n_),pre(n_),in_deg(n_,0),out_deg(n_,0),negative(false),dag(true),s(s_),t(t_){if(t<0)t=n-1;}voiduse_bellman_ford(){dag=false;}TFoperator[](constintedge_id)const{assert(G.is_prepared());constauto&[from,id]=edge_memo[edge_id];returnG.edge[from][id].weight.cap;}std::vector<std::tuple<int,int,TF,TC>>all_edge(){assert(G.is_prepared());std::vector<std::tuple<int,int,TF,TC>>res;res.reserve(edge_memo.size());for(constauto&[v,id]:edge_memo){constauto&[to,from,weight]=G[v][id];res.emplace_back(from,to,weight.cap,-weight.cost);}returnres;}voidadd_arc(intfrom,intto,TFcap,TCcost){G.add_arc(from,to,{cap,cost,out_deg[to]});G.add_arc(to,from,{0,-cost,out_deg[from]++});edge_memo.emplace_back(to,out_deg[to]++);if(cap>0){in_deg[to]++;negative|=cost<0;}}std::pair<TC,bool>flow(TFf=std::numeric_limits<TF>::max()){// second が 0// で返ってきた場合はそもそも最大流がfに達しないif(!G.is_prepared())G.build();TCres=0;std::ranges::fill(potential,0);// 一番最初は負のコストの辺が無いからポテンシャルは0にしていいwhile(f>0){if(negative)if(dag)DAG();elseBellmanFord();elsedijkstra();if(dist[t]==INF)returnstd::make_pair(res,false);REP_(v,n)if(dist[v]<INF)potential[v]+=dist[v];TFd=f;// d:今回流す量for(intv=t;v!=s;v=pre[v].first)chmin(d,(G[pre[v].first][pre[v].second].weight).cap);f-=d;res+=potential[t]*d;for(intv=t;v!=s;v=pre[v].first){auto&[cap,cost,rev]=G[pre[v].first][pre[v].second].weight;cap-=d;(G[v][rev].weight).cap+=d;}}// このループを抜けてるならf流れてるreturnstd::make_pair(res,true);}std::pair<TC,bool>st_flow(ints_,intt_,TFlim=std::numeric_limits<TF>::max()/2){s=s_;t=t_;returnflow(lim);}};#undef REP_
#line 6 "test/AOJ/GRL_6_B.test.cpp"
intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn,m,f;std::cin>>n>>m>>f;MCF<int,int>fl(n);while(m--){intu,v,c,d;std::cin>>u>>v>>c>>d;fl.add_arc(u,v,c,d);}auto[ans,ok]=fl.flow(f);std::cout<<(ok?ans:-1)<<std::endl;}